I’m trying to dropdown html

Asked

Viewed 40 times

1

I’m trying to implement a DropDownList in a View mine that serves for form, but I need the selected value to go to a ViewBag. This is my View there where it is "Car Selected", I would need to turn my Textbox in a DropDownList:

inserir a descrição da imagem aqui

That’s the code of my view:

<div class="form-group">
     <label for="carro" class="col-sm-2 control-label">Carro selecionado</label>
         <div class="col-sm-10">
           <input type="number" class="form-control" id="carro" name="carro" required>
         </div>
</div>

This is my Controller:

public ActionResult FormData()
{
    ViewBag.Veiculo = veiculoDAO.Lista();
    return View();
}

1 answer

1


Change your structures to these:

Html

<div class="form-group">
    <label for="carro" class="col-sm-2 control-label">Carro selecionado</label>
    <div class="col-sm-10">
        @Html.DropDownList("Veiculo")
    </div>
</div>

Code

public ActionResult FormData()
{
    var campoId = "nome do campo";
    var campoDescricao = "nome do campo Descricao"  
    ViewBag.Veiculo = new SelectList(veiculoDAO.Lista(), campoId , campoDescricao);
    return View();
}

References:

  • The field, is to put the bank ID and the description the column I want you to bring me?

  • @That’s the name of the two fields of your table!!! fieldId would be the name of the key, and fieldDescription the name of the description that will appear in the box! That is, your List returns which fields to be placed as key and description!

Browser other questions tagged

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