Applying a Bootstrap class to @Html.DropDownList()

20,198

You haven't supplied anything for the items in the dropdown.

Notice this part of the error:

System.Collections.Generic.IEnumerable

..you're missing an enumerable that can be used for the items. Change your code to this:

@Html.DropDownList("Scholarship_contactID", 
                   new SelectList(new List<string>()), 
                   "-- Select Contact --", 
                   new { @class = "form-control"})

This uses the following overload:

public static MvcHtmlString DropDownList(
    this HtmlHelper htmlHelper,
    string name,
    IEnumerable<SelectListItem> selectList,
    string optionLabel,
    object htmlAttributes
)
Share:
20,198
SRiley22
Author by

SRiley22

Updated on July 09, 2022

Comments

  • SRiley22
    SRiley22 almost 2 years

    I am trying to apply bootstrap styling to the following dropdown list:

    @Html.DropDownList("Scholarship_contactID", "-- Select Contact --", new { @class = "form-control"})
    

    This returns the following 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, System.Collections.Generic.IEnumerable, string)' has some invalid arguments