5

I have a form in ASP MVC (Razor) and in this form I have a Textboxes... and a <select> and I want to transmit the selected option (value) of this select to the controller like the other information. The textboxes are transmitted correctly to the controller and it's in the database.

I have a field in the database called NumberParticipant and I want to transmit i tin the database thanks to my Create controller :

My code :

@using (Html.BeginForm())
{
  <div class="form-group">
    @Html.LabelFor(model => model.LocalNumber, new { @class = "control-label col-md-2" })
    <div class="col-md-10">
      @Html.EditorFor(model => model.LocalNumber, new { htmlAttributes = new { @class = "form-control" } })
      @Html.ValidationMessageFor(model => model.LocalNumber)
    </div>
  </div>
  <div class="col-md-10">
    <select class="form-control" id="numberParticipant">
      <option value=""></option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
    </select>
  </div>
  <input type="submit" value="Create" class="btn btn-default" />
}
2
  • Your <select> has no name attribute so nothing is posted back. (and why not bind to a model property and use @Html.DropDownListFor()) Commented Dec 16, 2014 at 22:15
  • I think about the DropDownListFor but how can I put in this DropDownListFor the options 2,3,4,5 and the first option null ? Commented Dec 16, 2014 at 22:17

1 Answer 1

6

Using the MVC DropDownList helper would save you from writing out all the html. Here's an example that sets the option values in the view.

<div class="col-md-10">
    @Html.DropDownListFor(model => model.NumberParticipant, 
        new SelectList(new [] 
        { new {value="",text=""},
          new {value="2",text="2"},
          new {value="3",text="3"},
          new {value="4",text="4"},
          new {value="5",text="5"}
    },"value","text"), new { htmlAttributes = new { @class = "form-control" }})
</div>

A best practice would be to add a property of type SelectList to your model class and set the possible options there. You could then reference the new property in the helper like this:

<div class="col-md-10">
    @Html.DropDownListFor(model => model.NumberParticipant, 
        Model.MySelectList, new { htmlAttributes = new { @class = "form-control" }})
</div>
Sign up to request clarification or add additional context in comments.

4 Comments

new SelectList("","2","3","4","5"), I can't put 5 items in that
Good point! I've just edited my answer to correct the syntax for populating the SelectList. I'd recommend creating a SelectList property in the model and using the second approach though if possible.
My options don't have a value for example the 2 the value must be 2 <option value="2">2</option>
@user3524214, When you select the option with the display text of 2, then a value of 2 will be posted back (if there is no explicit value attribute, then the text is used).

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.