Skip to content

Instantly share code, notes, and snippets.

@bhameyie
Created May 28, 2013 22:17
Show Gist options
  • Save bhameyie/5666592 to your computer and use it in GitHub Desktop.
Save bhameyie/5666592 to your computer and use it in GitHub Desktop.
mvc enum helper
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