3

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?

2 Answers 2

2

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

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.