the real issue is that select list is used to build the options, and because multiselect is allowed, the selected property of the SelectListItem is used to set the selected property of the option. also all variances of the dropdown and listbox call the same
render routine, and the selectlist is the key input. the the dropdown and dropdownfor act as helper methods to set the selected options in the list.
I agree that an immutable list would be safer, but it would require a clone of the select list items (not the list). but C# has poor support for immutable, and you should not assume passed objects are not mutated.
All-Star
58464 Points
15788 Posts
Re: DropDownListFor modifying passed SelectListItems
Mar 06, 2020 09:07 PM|bruce (sqlwork.com)|LINK
the real issue is that select list is used to build the options, and because multiselect is allowed, the selected property of the SelectListItem is used to set the selected property of the option. also all variances of the dropdown and listbox call the same render routine, and the selectlist is the key input. the the dropdown and dropdownfor act as helper methods to set the selected options in the list.
I agree that an immutable list would be safer, but it would require a clone of the select list items (not the list). but C# has poor support for immutable, and you should not assume passed objects are not mutated.