I have a form in an ASP.NET MVC project which has a select drop down list akin to the following:
<select>
<option value="ee5711b9-ec86-4378-a975-ae10a4ebedbc">Volvo Account</option>
<option value="0dc0e9d8-2245-43de-81a9-5b94c19646fa">Saab Account</option>
<option value="f9a05ef6-9ca6-4eeb-9e04-79726a62b38c">Mercedes Account</option>
<option value="1c5c2e43-06d6-4b7d-916a-231be535a608">Audi Account</option>
</select>
In a later page in the project I need to do something with the GUID identifier, but first I need to prompt the user for confirmation. Obviously the GUID is useless to them and they’d like to see the friendlier name (i.e., “Volvo Account”).
But when I drill into the FormCollection values, all I can get is the value, not the text of the selected option This makes sense given the design goals, but how can I also POST over the text value?
I can think of some workarounds (setting a hidden field with JavaScript, doing a lookup after the fact with the GUID and the same method by which I populated this, etc.) but is there any intrinisc way to do this I’m missing?
There’s no intrinsic way to do this, JavaScript is really your best option.
Add an “onchange” event handler to your SELECT that sets the value of a hidden input to the display value of the selected OPTION.