Default value in Select List . net Core 2.2

Asked

Viewed 66 times

1

In my system I have a select which returns the recorded data in your database table, the problem is that the list already starts with the first record automatically, and I need her to return a standard text and below the table data for choice, for example "Select", do not know how to add this text by default in my select, follow the codes:

cshtml:

  <div class="form-group col-md-2">
      <label>PA</label>
      <select  class="form-control" id="PaId" asp-for="PaId" asp-items="ViewBag.PaId"></select>
      <span asp-validation-for="PaId" class="text-danger"></span>   
    </div>

Controller:

public IActionResult NovoChamado()
      {
          ViewData["PaId"] = new SelectList(_context.PAs,"PaId", "Nome");
          ViewData["FornecedorId"] = new SelectList(_context.Fornecedores, "FornecedorId","Empresa");
          ViewData["StatusId"] = new SelectList(_context.ChamadoStatus, "StatusId","Situacao");

          return View();
      }

Exemplo do sistema, o primeiro texto gostaria que fosse selecionar

1 answer

2


You can do it this way:

<div class="form-group col-md-2">
  <label>PA</label>
  <select  class="form-control" id="PaId" asp-for="PaId" asp-items="ViewBag.PaId">
    <option disabled selected>Selecionar...</option>
  </select>
  <span asp-validation-for="PaId" class="text-danger"></span>   
</div>

You can also at backend add an item with the default text you want to use in your list, but usually prefer the form above.

  • It worked, thank you very much

Browser other questions tagged

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