DropDownList setting selected item in asp.net MVC

前端 未结 3 630
长发绾君心
长发绾君心 2020-11-27 16:48

I noticed what seems to me a bug in asp.net MVC or simply I am doing something wrong. I am currently using 1.0 so maybe this is something that will be addressed in the 2.0 r

3条回答
  •  甜味超标
    2020-11-27 17:20

    I encountered this same problem using the Html.DropDownList(string name, IEnumerable selectList) overload. It appears that my model has a property of the same name as the name of the drop down list. This being the case, MVC favored the property value of my Model over the Selected property of each entry in the IEnumerable.

    The solution was to use a name for the dropdown list that does not match up to a property name. Another solution would be to write my own extension method that ignores model and view state and instead always honor the selected property.

提交回复
热议问题