Mark
Mark

Reputation: 7818

asp.net mvc3/4 request.form to return the selected value and the selected text from selectlist

How would I get in my controller, the text part of Request.Form["StandID"]

<div class="editor-field">
<select id="StandID" name="StandID">
<option value=""/>
<option value="3">Mark</option>
<option value="5" Selected>Brian</option>
<option value="6">Ian</option>
<option value="7">Vin</option>
</select>
</div>

So in my controller, Request.Form["StandID"] = 5

Is it possible to retrieve the text (I know this isn't posted as part of the form - is there a way to do that)?

So I could like to return "Brian" - as well as the ID of 5?

Thank you,

Mark

Upvotes: 0

Views: 630

Answers (2)

Felix
Felix

Reputation: 852

as an alternative you could use hidden input field to store your text value. The downside is you have to place your value into this hidden field with javascript (on selected change or on form submit), but pros is you will have separated field for value, so you don't have to parse it on server side and you can avoid possible problems with naming.

Upvotes: 0

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93434

No. HTML will only post the value to the controller. However, you could make your value be something like "5 - Brian" and then have your text be "Brian". Then you would have to parse the value to get the real value from it.

Upvotes: 3

Related Questions