How do I Dynamically Preselect an item in a html.DropDownlist in ASP.NET MVC

半城伤御伤魂 提交于 2019-12-05 01:34:26

问题


is there way thats i can preselect an item when the page loads or posts to the server..

this is what i have right now..

<%=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:


Set the SelectedValue property of the SelectList, or pass it as second parameter to SelectList constructor.

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


来源:https://stackoverflow.com/questions/450669/how-do-i-dynamically-preselect-an-item-in-a-html-dropdownlist-in-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!