Multiple dropdowns from one selectlist asp.net mvc
Asked Answered
E

0

1

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?
Eachern answered 30/5, 2013 at 19:13 Comment(4)
Example code would be useful... How are you generating your SelectList instances? Are you using ViewBag or no? How are you binding to a model and what model are you using (need to see the code)?Numbers
As @Numbers says, this sounds like a potential issue with the way you're binding your SelectLists but it is impossible to know without seeing your code.Hammerskjold
How did you end up resolving this issue?Vesica
I cached the values but I created a new SelectList for each dropdown.Eachern

© 2022 - 2024 — McMap. All rights reserved.