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
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 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.
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.
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.
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
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With