开发者

Another DropDownList selected item problem

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 wro开发者_运维百科ng?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜