Created
May 28, 2013 22:17
-
-
Save bhameyie/5666592 to your computer and use it in GitHub Desktop.
mvc enum helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class EnumHelper | |
{ | |
public static List<SelectListItem> GetEnumItems<T>(this T enumType) | |
{ | |
var values = (T[]) Enum.GetValues(typeof (T)); | |
return values.Select(e => new SelectListItem {Selected = enumType.Equals(e), Text = e.ToString(), Value = e.ToString()}).ToList(); | |
} | |
public static string GetEnumStrings<T>() | |
{ | |
var list= Enum.GetNames(typeof(T)); | |
var final = "["; | |
var lastindex = list.Length - 1; | |
for (var i = 0; i < lastindex; i++) | |
{ | |
final += SurroundWithQuotes(list.ElementAt(i)) + ","; | |
} | |
final += SurroundWithQuotes(list.ElementAt(lastindex)) + "]"; | |
return final; | |
} | |
private static string SurroundWithQuotes(string str) | |
{ | |
return "\"" + str + "\""; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment