I have a form on a View with a dropdown list, implemented with DropDownListFor(). This View is strongly typed to a ViewModel, which has a SelectList property to hold the options of the dropdown, and then another property to hold the selected value of the dropdown. This is working fine, but what I'd like to do is, hold both the selected value AND selected text of the dropdown in my second property. The reason I want to do this is so that as the form selfposts, I have both the text and value of each selection.
I tried changing the selected value property from an int to a KeyValuePair but only the int part of the pair is set on form submission.
Perhaps there is a better way altogether to accomplish this, I am open to all suggestions including a partial redesign of my methods.
Controller (building SelectList)
SelectList leadTypeGroups = new SelectList(_enrollmentRepository.GetLeadTypeGroups(), "Key", "Value");
ViewModel
public KeyValuePair<int, string> LeadTypeGroupID { get; set; }
public SelectList LeadTypeGroups { get; set; }
View
@Html.DropDownListFor(selected => Model.LeadTypeGroupID, Model.LeadTypeGroups, " ")
Change the ViewModel to a List and then use a for loop to spit out a drop down for each item in the list. Something like...
View
ViewModel
Have the sever pull the values from the database (or an in-memory cache) on each request. Alternatively, have a hidden field with javascript that updates it with the appropriate text whenever the dropdown selection changes.