views:

133

answers:

2

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

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?

A: 

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")

Tx3
A: 

This issue was started from MVC 1.0 at least.

Problem: when DropDownList Name has same value as name of object binded to it, selected option will be not selected in result.

Solution: Name binded object different, Model.SavingTypeList for example in your case. Renaming DDL Name is not preferred, because you may loose your binding after form submitting.

Lion_cl