I'm building my first MVC application after years of doing webforms, and for some reason I am not able to make this work:
@Html.DropDownList("PriorityID", String.Empty, new {@class="textbox"} )
Error message:
System.Web.Mvc.HtmlHelper<SPDR.Models.Bug>
' does not contain a definition for DropDownList
and the best extension method overload System.Web.Mvc.Html.SelectExtensions.DropDownList(System.Web.Mvc.HtmlHelper, string, System.Collections.Generic.IEnumerable<System.Web.Mvc.SelectListItem>, object)
has some invalid arguments
Any help greatly appreciated!
If you are add more than argument ya dropdownlist in Asp.Net MVC. When you Edit record or pass value in view bag.
Use this it will be work:-
Simply Try this
But if you want a default value or no option value then you must have to try this one, because
String.Empty
will select that no value for you which will work as a-select-
as default optionLooking at the controller, and learing a bit more about how MVC actually works, I was able to make sense of this.
My view was one of the auto-generated ones, and contained this line of code:
To add html attributes, I needed to do something like this:
Thanks again to @Laurent for your help, I realise the question wasn't as clear as it could have been...
UPDATE:
A better way of doing this would be to use DropDownListFor where possible, that way you don't rely on a magic string for the name attribute
As the signature from the error message implies, the second argument must be an IEnumerable, more specifically, an IEnumerable of SelectListItem. It is the list of choices. You can use the SelectList type, which is a IEnumerable of SelectListItem. For a list with no choices:
For a list with a few choices:
Maybe this tutorial can be of help: How to create a DropDownList with ASP.NET MVC