Closed Gabby-Paolucci closed 4 years ago
I'll also note, this was submitted as an issue for the project back in 2014 when it was hosted in CodePlex: ASP.NET MVC / Web API / Web Pages - View Issue #1913: DropdownListFor modifies the SelectListItem collection passed to it (Wayback Machine Archive)
It was determined that the developers were "we are concerned that some users might have taken dependency on the behavior (although this is probably rare)."
And there was a post about this in 2013 in the ASP.NET Forums: DropDownListFor modifying passed SelectListItems | The ASP.NET Forums
Thanks for contacting us. @Rick-Anderson can this be clarified in the ASP.NET Web Stack docs?
This issue was moved to dotnet/AspNetCore.Docs#18841
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
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.