Select ENUM Tag Helper in ASP.NET Core MVC

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

 public class ProspectLog
    {
        public int Id { get; set; }
        public int IdProspect { get; set; }
        public int IdEmpresa { get; set; }
        public DateTime Criado { get; set; }
        public string Usuario { get; set; }
        public string Descricao { get; set; }

        public ETipoLog TipoLog { get; set; }

        public enum ETipoLog
        {
            [Display(Name = "CADASTRO")]
            Cadastro = 0,
            [Display(Name = "CONTATO")]
            Contato = 1,
            [Display(Name = @"TROCA ETAPA")]
            Troca = 2,
            [Display(Name = @"QUALIFICAÇÃO")]
            Qualifica = 3,
            [Display(Name = @"EDIÇÃO")]
            Edicao = 4
        }
    }

On my old project based on MVC5 I just used this on my View and it was enough.

DROPDOWN

<div class="form-group col-sm-6">
   <label style="font-weight: bolder" for="txtSituacao">Situação</label>
   @Html.EnumDropDownListFor(model => model.Situacao, htmlAttributes: new { @class = "form-control" })
   @Html.ValidationMessageFor(model => model.Situacao, "", new { @class = "text-danger" })
</div>

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: enter image description here

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.


Solution 1:

Finally I found the solution!

It doesn't seem obvious, but that way I have no compilation errors!!! The answer I got from Ivan was not incorrect, but it was necessary to import the CRM.Model on the view like:

@using CRM.Model;

So, my dropdown:

<select asp-for="TipoLog" asp-items="Html.GetEnumSelectList<ETipoLog>()" class="form-control"></select>

enter image description here

You can see, Visual Studio told me it was unnecessary, painting it in grey, but without that, I get compilation error. I hope I can help someone else.

Solution 2:

You are forgetting to escape the C# code inside the HTML with "@"

Try:

<select asp-for="TipoLog" asp-items="Html.GetEnumSelectList<TipoLog>()"></select>

Updated to remove @ before Html.Get....

Solution 3:

This worked for me, without needing the using declaration.

<select asp-for="ClientFeeStage"
   asp-items="@Html.GetEnumSelectList(typeof(AT.Domain.Entities.ClientFeeStage))"
   class="form-control">

Solution 4:

Try <select asp-for="TipoLog" asp-items="Html.GetEnumSelectList<ETipoLog>()"></select>