Muokkaa

Jaa


MVC recommended tutorials and articles

by Rick Anderson

Getting Started

Working with data

Security

Azure

Performance and Debugging

ASP.NET MVC DropDownListFor with SelectListItem

When using the DropDownListFor helper and passing to it the collection of SelectListItem from which it is populated, the DropdownListFor modifies the passed collection after it is called. DropdownListFor changes the SelectListItems Selected properties to whatever was selected by the dropdown list. This leads to unexpected behavior.

The DropDownListFor, DropDownList, EnumDropDownListFor, ListBox, and ListBoxFor update the Selected property of any IEnumerable<SelectListItem> passed or found in ViewData.

The workaround is to create separate enumerables, containing distinct SelectListItem instances, for each property in the model.

For more information, see GetSelectListWithDefaultValue Modifies IEnumerable<SelectListItem> selectList