如何设置ASP.NET MVC查看页面上下拉的默认选定值 [英] How to set the default selected value of Dropdown on ASP.NET MVC View page
本文介绍了如何设置ASP.NET MVC查看页面上下拉的默认选定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个下拉菜单,我需要设置从数据库中选择默认值。
I have a dropdown that i need to set the default selected value from the database.
例如:
<%= Html.DropDownList("state", " -- Select one -- ")%>
以上code从我的实体模型结合从计算机[特产]的记录。
而选择一项在顶部增加。
现在,如果我需要我的下拉到默认值设置为'NY'。我该怎么做?
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?
鸭preciate你的反应。
Appreciate your responses.
感谢
推荐答案
转至视图模型一个的SelectList
Pass in a SelectList in the ViewModel
ViewData["States"] = new SelectList(states, "State_ID", "Name", "State_ID");
然后
<%= Html.DropDownList("State_ID", (IEnumerable<SelectListItem>)ViewData["States"])%>
这篇关于如何设置ASP.NET MVC查看页面上下拉的默认选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文