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

后端 未结 30 2446
不知归路
不知归路 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:54

    If you want to add localization support just change the s.toString() method to something like this:

    ResourceManager rManager = new ResourceManager(typeof(Resources));
    var dayTypes = from OperatorCalendarDay.OperatorDayType s in Enum.GetValues(typeof(OperatorCalendarDay.OperatorDayType))
                   select new { ID = s, Name = rManager.GetString(s.ToString()) };
    

    In here the typeof(Resources) is the resource you want to load, and then you get the localized String, also useful if your enumerator has values with multiple words.

提交回复
热议问题