Description
The GetSelectListWithDefaultValue method modifies the selectList given as a parameter. A copy of the list is made, but selectList is modified (specifically the Selected properties) as part of the process and since the object is passed by reference, the original object is modified. This is confusing behavior.
src/System.Web.Mvc/Html/SelectExtensions.cs
private static IEnumerable<SelectListItem> GetSelectListWithDefaultValue(IEnumerable<SelectListItem> selectList, object defaultValue, bool allowMultiple)
{
...
List<SelectListItem> newSelectList = new List<SelectListItem>();
foreach (SelectListItem item in selectList)
{
item.Selected = (item.Value != null) ? selectedValues.Contains(item.Value) : selectedValues.Contains(item.Text);
newSelectList.Add(item);
}
return newSelectList;
}
Here's an example of where this can cause confusion:
@Html.DropDownListFor(model => model.Field1, Model.SelectListItems)
@Html.DropDownListFor(model => model.Field2, Model.SelectListItems)
If Field1 has a value set but Field2 is null, Field1 will still render with the value of Field1 set since Model.SelectListItems will have had Selected set to true for that SelectListItem.
If anything, the documentation should be updated to note this effect. Resolving this bug could be a breaking change—I imagine there is existing code that depends on this behavior.