I am really confused about ASP.Net MVC (2.0 RC) DropDownList. It doesn’t seem to accept my selected item.
Here is my SelectListItem list (it is created inside loop. Selected being boolean)
savingTypesList.Add(new SelectListItem() { Selected = selected, Text = type.Name, Value = "" + type.SavingTypeId });
Creating SelectList itself..
return new SelectList(savingTypesList, "Value", "Text", (string)selected);
And my View..
<p>
<label for="SavingType">Saving type</label>
<%= Html.DropDownList("SavingType", Model.SavingType, "-- select a parameter saving type --", "")%>
<%= Html.ValidationMessage("SavingType", "*")%>
</p>
When I check details of the returned SelectList it show me following information
alt text http://www.tx3.fi/selected.PNG
As you can see SelectedValue is set correctly and it matches to the item in the Items array. When I check the actual generated HTML there isn’t any selected. What’s wrong?
I was able to solve this problem on my own.
As you can see on my View I had DropDownList with name “SavingType” and it was binded to the Model.SavingType. Everything works fine except the selected value. Only thing I did was that I changed the name of the dropdownlist (and of course links to it like label for=”SavingTypeList”)