How to set the default selected value of Dropdown on ASP.NET MVC View page
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"])%>
精彩评论