How to perform submit select list view for Controller?

0

I have the following code in my view:

    @using (Html.BeginForm("DataAniv", "Mailing", FormMethod.Post))
{
    <div class="form-group">
        <label for="sel1">Selecione o mês:</label>
        <select class="form-control" id="sel1">
            <option value="Janeiro">Janeiro</option>
            <option value="Fevereiro">Fevereiro</option>
            <option value="Março">Março</option>
            <option value="Abril">Abril</option>
            <option value="Maio">Maio</option>
            <option value="Junho">Junho</option>
            <option value="Julho">Julho</option>
            <option value="Agosto">Agosto</option>
            <option value="Setembro">Setembro</option>
            <option value="Outubro">Outubro</option>
            <option value="Novembro">Novembro</option>
            <option value="Dezembro">Dezembro</option>
        </select>
    </div>
    <input class="btn btn-primary" type="submit" value="Gerar">
}

The idea is that once the user makes a selection in this list, the "value" is sent to the controller that will execute a logic. However, it does not matter which way I try, the form always gives the submit with null value. Is it possible for the code to work this way? Or should I use Javascript or @Html.DropdownListFor?

    
asked by anonymous 02.08.2018 / 16:16

1 answer

1

To receive the value of your select in the Controller, you need to declare the name attribute. The id attribute has "only" utility for javascript and css, if the input does not have a defined name it will not be included in the FormData being posted to the server.

<select class="form-control" id="sel1" name="sel1">

See the example below:

$("#forumlario").on('submit', function() {

  var formData = new FormData(document.forms[0]);

  for (var pair of formData.entries()) {
    console.log('Input:"'+ pair[0] + '", Valor "' + pair[1]+"'");
  }

  return false;

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><formmethod="post" id="forumlario">
  <input type="text" id="Campo1" name="CampoName" value="campo com name" />  
  <input type="text" id="Campo3" value="campo sem name" />
  <br>
          <select class="form-control" id="sel1" name="sel1">
            <option value="Janeiro">Janeiro</option>
            <option value="Fevereiro">Fevereiro</option>
            <option value="Março">Março</option>
            <option value="Abril">Abril</option>
            <option value="Maio">Maio</option>
            <option value="Junho">Junho</option>
            <option value="Julho">Julho</option>
            <option value="Agosto">Agosto</option>
            <option value="Setembro">Setembro</option>
            <option value="Outubro">Outubro</option>
            <option value="Novembro">Novembro</option>
            <option value="Dezembro">Dezembro</option>
        </select>
  <br>
  <button type="submit">Enviar</button>

</form>
    
02.08.2018 / 16:24