Enum to SelectList


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  1. public static SelectList ToSelectList(this Enum enumeration)
  2. {
  3. return enumeration.ToSelectList(false);
  4. }
  5.  
  6. public static SelectList ToSelectList(this Enum enumeration, bool useSelectedValues)
  7. {
  8. var values = new Dictionary<string, string>();
  9. foreach (Enum e in Enum.GetValues(enumeration.GetType()))
  10. {
  11. values.Add(e.GetDisplayName(), e.ToString());
  12. }
  13. SelectList list;
  14. if (useSelectedValues)
  15. {
  16. list = new SelectList(values, "value", "key", values[enumeration.ToString()]);
  17. }
  18. else
  19. {
  20. list = new SelectList(values, "value", "key");
  21. }
  22. return list;
  23. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.