public static SelectList GetSelectListForEnumWithDescription(Type enumType)
{
var list = new List<Object>();
Array enumValues = Enum.GetValues(enumType);
foreach (Enum enumValue in enumValues)
{
list.Add(new
{
ID = (int)Enum.Parse(enumType, enumValue.ToString()),
Value = GetEnumDescription(enumValue)});
}
return new SelectList(list, "ID", "Value");
}
public static string GetEnumDescription(Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[]) fi.GetCustomAttributes(typeof (DescriptionAttribute), false);
if (attributes != null && attributes.Length > 0)
return attributes[0].Description;
else return value.ToString();
}
}
{
var list = new List<Object>();
Array enumValues = Enum.GetValues(enumType);
foreach (Enum enumValue in enumValues)
{
list.Add(new
{
ID = (int)Enum.Parse(enumType, enumValue.ToString()),
Value = GetEnumDescription(enumValue)});
}
return new SelectList(list, "ID", "Value");
}
public static string GetEnumDescription(Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[]) fi.GetCustomAttributes(typeof (DescriptionAttribute), false);
if (attributes != null && attributes.Length > 0)
return attributes[0].Description;
else return value.ToString();
}
}