– ASP.NET MVC Html.DropDownList SelectedValue

I have tried this is RC1 and then upgraded to RC2 which did not resolve the issue.

// in my controller
ViewData["UserId"] = new SelectList(
    selectedUserId.Value); // this has a value

result: the SelectedValue property is set on the object

// in my view
<%=Html.DropDownList("UserId", (SelectList)ViewData["UserId"])%>

result: all expected options are rendered to the client, but the selected attribute is not set. The item in SelectedValue exists within the list, but the first item in the list is always defaulted to selected.

How should I be doing this?

Thanks to John Feminella's reply I found out what the issue is. "UserId" is a property in the Model my View is strongly typed to. When Html.DropDownList("UserId" is changed to any other name but "UserId", the selected value is rendered correctly.

This results in the value not being bound to the model though.

Best Solution

This is how I fixed this problem:

I had the following:


ViewData["DealerTypes"] = Helper.SetSelectedValue(listOfValues, selectedValue) ;


<%=Html.DropDownList("DealerTypes", ViewData["DealerTypes"] as SelectList)%>

Changed by the following:


<%=Html.DropDownList("DealerTypesDD", ViewData["DealerTypes"] as SelectList)%>

It appears that the DropDown must not have the same name has the ViewData name :S weird but it worked.