views:

308

answers:

3

I am using a dropdown list as follows.

<%=Html.DropDownList("ddl", ViewData["Available"] as SelectList, 
   new { CssClass = "input-config", onchange = "this.form.submit();" })%>

On its selection change I am invoking post action. After the post the same page is shown on which this drop down is present. I want to know about the HTML attribute for the drop down which will let me preserve the list selection change. But as of now the list shows its first element after the post. e.g. The dropdoen contains elements like 1,2,3,etc. By default 1 is selected. If I select 2, the post is invoked and the same page is shown again but my selection 2 goes and 1 is selected again. How can preserve the selection?

Thanks, Kapil

A: 

You have to make the list of select list items again and tell which of the items is the selected one in every post (Selected property of the SelectListItem).

uvita
A: 

When you perform the post you will be setting the ViewData["Available"] again, you can set the select item here. So when you create the drop down list in the html the selected item is already selected. So your code could look something like:

ViewData["Available"] = new SelectList( items, "dataValueField", "dataTextField", "selectedValue" );
Simon G
A: 

Look at this answer for a possible approach:

http://stackoverflow.com/questions/2192604/help-with-asp-net-mvc-select-dropdown/2192703#2192703

Serhat Özgel