Data selected from <option> tag to pass to Model C# - MVC

Asked

Viewed 72 times

1

I am developing an application with HTML and C# in the MVC standards, and in one of the menus I need to put a list of options in which the user will select what he wants, this data comes from the database, so what I did was the following:

I created a Model and there I created a list.

public class NotasFiscaisModel
    {
        public virtual string usuarioLogado { get; set; }
        public virtual string dataUltimoLogin { get; set; }
        public virtual string unidadeInicio { get; set; }
        public virtual string unidadeFim { get; set; }
        public virtual string dataInicial { get; set; }
        public virtual string dataFinal { get; set; }
        public virtual string nome { get; set; }
        public virtual IList<NotasFiscaisModel> autorizados { get; set; }
    }

With this, I select to popular my authorized list

     foreach (DataRow linha in autoriz.RetornaAutorizados(LoginController._login.empresa).Rows)
  {
     NotasFiscaisModel nota = new NotasFiscaisModel();
     nota.nome = linha["nome"].ToString();
     _notas.autorizados.Add(nota);
  }

Inside HTML, I have an excerpt that lists the options via option tag:

    <td>
      <div class="col-xs-2" style="width:190px">
           <div class="form-group">
              <select class="form-control" id="sel1">
              <option>Todos</option>
              @foreach (var obj in Model.autorizados)
              {
              <option>@obj.nome</option>
              }
              </select>
         </div>
     </div>
</td>

This works, as it shows what is happening in the print below (do not mind the values, is kkk test base):

inserir a descrição da imagem aqui

Now I would like to take which user selected, returning to my Model, making the Controller have access to this content and be able to do more operations, there is a way?

  • Set a name for your select <select class="form-control" id="sel1" name="sel1"> ). https://stackoverflow.com/questions/2378338/how-to-get-selected-value-of-a-html-select-with-asp-net

  • @Marcelovieira worked, thank you very much! =)

  • @Marcelovieira would be interesting if you add the answer, so Bruno can give accepted answer and the solution is for the community ;)

1 answer

0


Browser other questions tagged

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