Posted By

nkirkes on 12/04/10


Tagged


Versions (?)

Enum to SelectList


 / Published in: C#
 

  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  

You need to login to post a comment.