Reputation: 605
I have a form which includes a few input elements and a few select elements. The point is that for the input elements I provide them a name attribute and later use it in my controller to get their value. But I don't know how can I get the value of a select element, and give it to the controller. Here is just a small snippet from my form:
<div class="form-group">
<label for="state">Price range</label>
<select name="price" class="form-control">
<option>$ 175,000 - $ 200,000</option>
<option>$ 200,000 - $ 250,000</option>
<option>$ 250,000 - $ 300,000</option>
<option>$ 300,000 - $ 350,000</option>
<option>$ 350,000 - $ 400,000</option>
</select>
</div>
<div class="form-group">
<label for="hear">What interests you?</label>
<input type="text" name="interests" class="form-control" id="inputHear" placeholder="What interests you? ">
</div>
Then, in my controller I have something like this:
[HttpPost]
public ActionResult Contact(..., string interests = "")
{
...
}
Can someone tell me how can I get the selected value from my select element?
Upvotes: 1
Views: 1663
Reputation: 2840
The text you can actually see is for the user, the value you are actually sending should be the contents of the value attribute on each option tag:
<div class="form-group">
<label for="state">Price range</label>
<select name="price" class="form-control">
<option value="175,000 - 200,000">$ 175,000 - $ 200,000</option>
<option value="200,000 - 250,000">$ 200,000 - $ 250,000</option>
<option value="250,000 - 300,000">$ 250,000 - $ 300,000</option>
<option value="300,000 - 350,000">$ 300,000 - $ 350,000</option>
<option value="350,000 - 400,000">$ 350,000 - $ 400,000</option>
</select>
</div>
<div class="form-group">
<label for="hear">What interests you?</label>
<input type="text" name="interests" class="form-control" id="inputHear" placeholder="What interests you? ">
</div>
And then in your controller:
[HttpPost]
public ActionResult Contact(string price, string interests = "")
{
...
}
Upvotes: 1