How do you create a dropdownlist from an enum in ASP.NET MVC?

后端 未结 30 1892
不知归路
不知归路 2020-11-21 16:36

I\'m trying to use the Html.DropDownList extension method but can\'t figure out how to use it with an enumeration.

Let\'s say I have an enumeration like

30条回答
  •  死守一世寂寞
    2020-11-21 17:03

    In ASP.NET MVC 5.1, they added the EnumDropDownListFor() helper, so no need for custom extensions:

    Model:

    public enum MyEnum
    {
        [Display(Name = "First Value - desc..")]
        FirstValue,
        [Display(Name = "Second Value - desc...")]
        SecondValue
    }
    

    View:

    @Html.EnumDropDownListFor(model => model.MyEnum)
    

    Using Tag Helper (ASP.NET MVC 6):

    
                            
        
    提交评论

提交回复
热议问题