ASP.NET MVC Dropdownlist retain the selected value in the browser after post

Posted by MLabib on Stack Overflow See other posts from Stack Overflow or by MLabib
Published on 2010-05-19T15:35:18Z Indexed on 2010/05/19 15:40 UTC
Read the original article Hit count: 224

Filed under:
|

I build a drop down list in my aspx page as following

<%= Html.DropDownList("SelectedRole", new SelectList((IEnumerable)Model.roles, "RoleId", "RoleName", Model.SelectedRole), "")%>

it works fine for first Get and the first default value is selected; then I select item from the drop down list and submit the form.

the controller bind the values correctly,

public ActionResult About([Bind] Roles r)
{
    //r.SelectedRole = the selected value in the page.
    //Roles r = new Roles();
    r.roles = new List<Role>();
    r.roles.Add(new Role(1, "one"));
    r.roles.Add(new Role(2, "two"));
    r.roles.Add(new Role(3, "three"));
    r.roles.Add(new Role(4, "four"));
    r.SelectedRole = null;
    return View(r)
}

Then I nullify the selected item and return my view, but still the previous selected Item is selected (although I did nullify it)

Any Idea if I am doing something wrong or it is a bug in MVC?

I am using ASP.NET MVC 1

Thanks

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about mvc