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

后端 未结 30 2322
不知归路
不知归路 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 16:45

    So without Extension functions if you are looking for simple and easy.. This is what I did

    <%= Html.DropDownListFor(x => x.CurrentAddress.State, new SelectList(Enum.GetValues(typeof(XXXXX.Sites.YYYY.Models.State))))%>
    

    where XXXXX.Sites.YYYY.Models.State is an enum

    Probably better to do helper function, but when time is short this will get the job done.

提交回复
热议问题