Select field with first blank item

0

I use select and it is populated from the database:

    <select asp-for="CategoriaID" asp-items="@Model.CountryList" id="cbcategoria" class="form-control">
                    </select>

And here's how it's in the controller:

model.CountryList = new SelectList(countries, "Id", "Value");

I would like to know how I can leave the unfilled ones when inserting a new record? The field is required in the ViewModel.

    
asked by anonymous 24.09.2018 / 15:46

2 answers

1

You can add an option with the disabled option so that it is not selected.

<select asp-for="CategoriaID" asp-items="@Model.CountryList" id="cbcategoria" class="form-control">
    <option disabled selected>Selecione</option>
</select>
    
24.09.2018 / 16:00
1

From what I understand you want the first select item to be white, you can do in the page load.

$("#cbcategoria").append("<option id='option_cbcategoria' value='' selected>" + "" + " </option>");
    
24.09.2018 / 15:53