Pass Select html value to Controller via Post

Asked

Viewed 1,539 times

1

I have a form where I pass some fields via Helper. inserir a descrição da imagem aqui

Ex:

<div class="form-group col-md-6">
     @Html.LabelFor(model => model.Nome, new { @class = "control-label col-md- 6" })
     @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class   = " form-control", @placeholder = "Digite um Nome" } })
     @Html.ValidationMessageFor(model => model.Nome)
</div>

Until then quiet, put in mine VIEW I also have a SELECT:

<div class="row">
    <div class="form-group col-md-3">
    @Html.Label("Pais", new { @class = "col-md-3" })
    <select id="cmbPais" class="form-control">
    <option>Carregar Paises</option>
    </select>
    </div>
    <div class="form-group col-md-4">
    @Html.Label("Estado", new { @class = "col-md-3" })
    <select id="cmbEstado" class="form-control">
    <option>Carregar Estados</option>
    </select>

<div class="form-group col-md-4">
     @Html.Label("Cidade", new { @class = "col-md-3" })
      <select id="cmbCidade" class="form-control">
      <option>Carregar Cidades</option>
   </select>
  </div>
 </div>
</div>

where I need to pass the selected value to the controller to register the user. How can I do this ?

  • What is the name of the field Estado in his @model? IS cmbEstado even?

  • I edited the question. what I want to pass to the Controller is a Cidadeid value. What I get via Jquery.

2 answers

2

If you want to pass CidadeId to the Controller, use the appropriate variable name already does it for you:

<select id="CidadeId" class="form-control">
    <option>Carregar Cidades</option>
</select>

Or you can use Razor:

@Html.DropDown("CidadeId", /* Coloque aqui a lista que populará a DropDown */)

1


How are you loading the list of cities?

If Cidadeid exists in your Model, you can replace the attribute select by the Htmlhelper DropDownFor, thus:

@Html.DropDownListFor(model => model.CidadeId, <sua lista>, "Carregar Cidades")

Razor will thereby create an attribute select with all the options passed in the list of the second parameter, and bind to the property of your model.

Explaining the parameters:

  • The first parameter will store the value that is selected in list.
  • The second parameter will be your list. Note that it is of type IEnumerable<SelectListItem> so if you carry it on Controller, it needs to be this type. If you load via jQuery (only including options in the attribute select), don’t worry with that property and just pass new List<SelectListItem>().
  • The third parameter is the value to display if no item be selected.

If you don’t have Cidadeid in your model, use Htmlhelper DropDownList in place, in this way:

 @Html.DropDownList("CidadeId",  <sua lista>, "Carregar Cidades")

So just create a parameter called CidadeId in the POST of your Controller and it will pass the selected value to this parameter

Browser other questions tagged

You are not signed in. Login or sign up in order to post.