Multiple dropdowns from one selectlist asp.net mvc

*爱你&永不变心* 提交于 2020-01-15 12:24:10

问题


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?

来源:https://stackoverflow.com/questions/16844121/multiple-dropdowns-from-one-selectlist-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!