开发者

how to add a "Please select" item to a dropdown box in asp.net mvc

开发者 https://www.devze.com 2022-12-26 19:34 出处:网络
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 = \"a开发者_高级运维utoComplet

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 = "a开发者_高级运维utoComplete1" })%>

i want to add an element at the top of the dropdown as the first item that says, "Please select".

do i have to add that into my Model.Items or is there a way i can add that element into the view and ignore the selection of that first element ??


You can do:

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

Its a little long to read, but the method signature is:

DropDownList(name, IEnumerable<SelectListItem>, optionLabel, htmlAttributes)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号