@Html.EnumDropDownListFor in Asp.net Core

后端 未结 4 1554
隐瞒了意图╮
隐瞒了意图╮ 2021-02-07 08:48

I\'m porting an MVC5 app in Asp.net Core. I cannot find

@Html.EnumDropDownListFor()

was it deprecated and is there a substitute? Is there any doc

相关标签:
4条回答
  • 2021-02-07 09:24

    This is a pending feature. Tracking issue: https://github.com/aspnet/Mvc/issues/438

    0 讨论(0)
  • 2021-02-07 09:32

    For anyone who is still looking for an answer, in ASP.NET 5 the functionality of EnumDropDownListFor() is obtained using DropDownListFor() in combination with GetEnumSelectList() method. For example:

    @model Enum
    @Html.DropDownListFor(m => m, Html.GetEnumSelectList(Model.GetType()))
    

    Note that you can decorate each value of the Enumeration with custom display names e.g. to include spaces. For instance:

    public enum CementTypes {
        [Display(Name = "Class S")]Class_S,
        [Display(Name = "Class N")]Class_N,
        [Display(Name = "Class R")]Class_R 
    }
    
    0 讨论(0)
  • 2021-02-07 09:35

    simply use asp-items="Html.GetEnumSelectList(typeof (State))" tag helper

    <select asp-for="State" asp-items="Html.GetEnumSelectList(typeof (State))"></select>
    
    0 讨论(0)
  • 2021-02-07 09:38

    Or

    @model Enum
    @Html.DropDownListFor(m => m, Html.GetEnumSelectList(typeof(Enum)))
    
    0 讨论(0)
提交回复
热议问题