如何动态预选ASP.NET MVC中的html.DropDownlist中的项目

时间:2009-01-16 15:08:15

标签: asp.net-mvc html-select

是否可以在页面加载或发布到服务器时预选项目。

这就是我现在所拥有的......

<%=Html.DropDownList("dllMonths", 
    new SelectList(new List<string>() {
       "", "January", "Feburary", "March", "April", "June", 
       "July", "August", "September", "October", "November", 
       "December"}), 
    new { onchange="this.form.submit();" })%>

1 个答案:

答案 0 :(得分:12)

设置SelectList的SelectedValue属性,或将其作为第二个参数传递给SelectList构造函数。

<% = Html.DropDownList ( "dllMonths", 
                           new SelectList ( new List ( ) 
                                          { "", "January", "Feburary", "March", 
                                            "April", "June", "July", "August", 
                                            "September", "October", "November", 
                                            "December" },
                                          "April" ), 
                           new { onchange = "this.form.submit();" } 
                       )%>