Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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();" })%>
like image 614
devforall Avatar asked Dec 18 '22 09:12

devforall


1 Answers

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();" } 
                       )%>
like image 65
baretta Avatar answered Jan 05 '23 00:01

baretta