Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying a Bootstrap class to @Html.DropDownList()

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

like image 245
SRiley22 Avatar asked Apr 02 '14 21:04

SRiley22


People also ask

How do I add a dropdown list in bootstrap?

Basic Dropdown To open the dropdown menu, use a button or a link with a class of . dropdown-toggle and the data-toggle="dropdown" attribute. Add the . dropdown-menu class to a <div> element to actually build the dropdown menu.

Why is my dropdown menu not working bootstrap?

Why is my drop down menu not working in bootstrap? Solution : The dropdown should be toggled via data attributes or using javascript. In the above program, we have forgotten to add a data attribute so the dropdown is not working. So add data-bs-toggle="dropdown" to toggle the dropdown.

How do I create a dropdown toggle in HTML?

Use any element to open the dropdown menu, e.g. a <button>, <a> or <p> element. Use a container element (like <div>) to create the dropdown menu and add the dropdown links inside it. Wrap a <div> element around the button and the <div> to position the dropdown menu correctly with CSS.

How can you use dropdown plugin in web page in bootstrap?

Using Dropdown plugin you can add dropdown menus to any components like navbars, tabs, pills and buttons. If you want to include this plugin functionality individually, then you will need dropdown. js. Else, as mentioned in the chapter Bootstrap Plugins Overview, you can include bootstrap.


1 Answers

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
)
like image 92
Simon Whitehead Avatar answered Nov 15 '22 15:11

Simon Whitehead