I need some help with an ENUM dropdown using Tag Helper.
I found lots exemples binding a model to Selectlist and some using ENUM but all of them, about CREATE action, and Im facing problems with EDIT action.
MY MODEL
On my old project based on MVC5 I just used this on my View and it was enough.
DROPDOWN
I tried with difent ways and I coudnt set the dropdown with database item select on Edit action. I tried this way:
<div class="form-group"> <label asp-for="TipoLog" class="col-md-2 control-label"></label> <div class="col-md-10"> <select asp-for="TipoLog" class="form-control"></select> <span asp-validation-for="TipoLog" class="text-danger"></span> </div> </div> I also tried like that:
<div class="form-group"> <label asp-for="TipoLog" class="col-md-2 control-label"></label> <div class="col-md-10"> <select asp-for="TipoLog" asp-items="Html.GetEnumSelectList<TipoLog>()"></select> <span asp-validation-for="TipoLog" class="text-danger"></span> </div> </div> But it ran me to an compilation error:
I also tryed to bind a model a list to a ViewBag on my controller, this way:
CONTROLLER:
ViewBag.Log = new SelectList(lista, "Id", "Nome"); VIEW:
<div class="form-group col-sm-2"> <label asp-for="TipoLogo" class="col-md-2 control-label"></label> <select asp-for="TipoLogo" asp-items="ViewBag.Log" class="form-control"></select> <span asp-validation-for="TipoLogo" class="text-danger"></span> </div> Its worked partially, the drop down listed the items, but not selecting the correct item from database. it show the first on the list as selected.