How can I convert an enumeration into a List?

后端 未结 7 1162
难免孤独
难免孤独 2021-01-30 10:31

i have an asp.net-mvc webpage and i want to show a dropdown list that is based off an enum. I want to show the text of each enum item and the id being the int value that the en

7条回答
  •  闹比i
    闹比i (楼主)
    2021-01-30 10:56

    I did this using a static method that I could reuse.

    public static IEnumerable EnumToSelectList()
    {
        return (Enum.GetValues(typeof(T)).Cast().Select(
            e => new SelectListItem() { Text = e.ToString(), Value = e.ToString() })).ToList();
    }
    

提交回复
热议问题