views:

113

answers:

1

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

+1  A: 

This is the normal behavior of all html helpers: they will look at the POSTed values to perform the binding. This means that you cannot change the value in your controller action and expect it to reflect on the view if you use the standard helpers. If there's a value SelectedRole in the POST it will always be this value used and the last parameter of the drop down completely ignored.

You could write your own helper to achieve this or redirect after the POST.

Darin Dimitrov