Reputation: 895
After adding the class to the html.dropdownlist am facing the below error.
Error:'System.Web.Mvc.HtmlHelper' does not contain a definition for 'DropDownList' and the best extension method overload 'System.Web.Mvc.Html.SelectExtensions.DropDownList(System.Web.Mvc.HtmlHelper, string, string)' has some invalid arguments
<li>
@Html.LabelFor(m => m.BuildType)
@Html.DropDownList("BuildType", new { @class = "BuildType" })
@Html.ValidationMessageFor(m => m.BuildType)
</li>
<li>@Html.LabelFor(m => m.BuildMode)
@Html.DropDownList("BuildMode", new { @class = "BuildMode" })
@Html.ValidationMessageFor(m => m.BuildMode) </li>
<li>
Upvotes: 0
Views: 596
Reputation: 14133
You can use:
@Html.DropDownListFor(m => m.BuildType, (SelectList)Viewbag.YourSelectList, "Select Build type", new { @class = "BuildType" })
or
@Html.DropDownListFor(m => m.BuildType, Model.YourSelectList, "Select Build type", new { @class = "BuildType" })
When you use @Html.DropDownList
, you specify a name for the dropdownlist... but you are missing the SelectList itself. I think that out of the box, the helper will try to use the name of the DropDownList (in your case "BuildType") to search in the ViewData collection for the SelectList.
When you use a @Html.DropDownListFor
you don't use a name, but a lamda expression m => m.BuildType
that will help you in same cases to not have harcoded names.
Your SelectList
(the second parameter) can be grabbed from Viewbag
or from a property in your Model
.
Upvotes: 1
Reputation: 15513
Where are your list options? You need to provide a list of options via an IEnumerable<SelectListItem>
object (see this overload).
So your model would have something like this:
public IEnumerable<SelectListItem> BuildModeOptions { get; set; }
And your view would pass the list into the helper:
@Html.DropDownList("BuildMode", Model.BuildModeOptions, new { @class = "BuildType" })
Or, since you're using the type-safe For
versions on your other helpers, use DropDownListFor
on this one as well:
@Html.DropDownListFor(m => m.BuildMode, Model.BuildModeOptions, new { @class = "BuildType" })
But keep in mind, Model.BuildMode is your selected value -- Model.BuildModeOptions is for your dropdown options.
Upvotes: 2
Reputation: 1982
The second parameter should be the list of items you want to show in the dropdown.
so It will look like :
@Html.DropDownListFor("BuildType", m.yourListofItems, new { @class = "BuildType" })
Upvotes: 1