How can I convert an enumeration into a List?

后端 未结 7 1161
难免孤独
难免孤独 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条回答
  •  深忆病人
    2021-01-30 10:36

    You can use Enum.GetNames() to get a string array containing the names of the enum items. If your item names are user friendly, then this is probably good enough. Otherwise, you could create your own GetName() method that would return a nice name for each item.

    OR - if the enum will never (or rarely) change, you could just create a method that directly adds hard-coded items to your dropdown. This is probably more efficient (if that is important to you).

提交回复
热议问题