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?
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)? Commented May 30, 2013 at 21:29
  • As @Marko 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. Commented May 30, 2013 at 22:22
  • How did you end up resolving this issue? Commented May 29, 2014 at 5:33
  • I cached the values but I created a new SelectList for each dropdown. Commented May 30, 2014 at 10:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.