-->

Multiple dropdowns from one selectlist asp.net mvc

2019-08-06 00:08发布

问题:

I just ran into an issue that I have never noticed before and I want to know if this is something isolated to my current project for some reason or if this is an issue that I need to panic about and check every one of my applications and correct right away.

I have two drop downs on the same page using the same select list.

ex. HomeCity and CurrentCity both populated from the same list of cities. Therefore in code both populated from the same IEnumerable<selectListItem>

It turns out that if the second value on the page is null (in the view model) then instead of defaulting to the optionLabel it defaults to whatever the first value is.

This issue is much exasperated when (as I often do) you try to cache the SelectList. Not only is the next item on the same page set to the wrong value, but the selected item gets changed in the cache (which is so strange because you would imagine that the cache serializes the values and give you a copy.

So my question is twofold.

  1. is this the usual functionality or did I trigger this weird behavior somehow?
  2. What is the recommended way to avoid this? Would I need to do a deep clone of my select list between dropdowns?