开发者

How to set the default selected value of Dropdown on ASP.NET MVC View page

开发者 https://www.devze.com 2022-12-19 01:51 出处:网络
I have a dropdown that i need to set the default selected value from the database. Example: <%= Html.DropDownList(\"state\", \" -- Select one -- \")%>

I have a dropdown that i need to set the default selected value from the database.

Example:

<%= Html.DropDownList("state", " -- Select one -- ")%>

The above code binds records from the ViewData["开发者_如何学编程specialty"] from my entity model. And "Select One" is added on the top. Now if i need my dropdown to set the default value to 'NY'. How can i do that?

Appreciate your responses.

Thanks


Pass in a SelectList in the ViewModel

ViewData["States"] = new SelectList(states, "State_ID", "Name", "State_ID");

then

<%= Html.DropDownList("State_ID", (IEnumerable<SelectListItem>)ViewData["States"])%>
0

精彩评论

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