How to add a "Please Select" item from the drop-down list in asp.net mvc

I have the following code in an asp.net mvc view.

<% = Html.DropDownList("Filter", new SelectList(Model.Items, "Id", "Name", 0), new { @id = "Filter", @class = "autoComplete1" })%>

I want to add an item at the top of the dropdown as the first item that says: "Please select."

Do I need to add this to my Model.Items or is there a way I can add this element to the view and ignore the selection of this first element ??

+5
source share
1 answer

You can do:

<% = Html.DropDownList("Filter", new SelectList(Model.Items, "Id", "Name", 0), "Please Select", new { @id = "Filter", @class = "autoComplete1" })%>

Its a bit long to read, but the signature of the method:

DropDownList(name, IEnumerable<SelectListItem>, optionLabel, htmlAttributes)
+16
source

All Articles