public static List<SelectListItem> GetSelectItemList<T>(string textDefault,string valueDefault="")
{
List<SelectListItem> list = new List<SelectListItem>();
list.Add(new SelectListItem() { Text = textDefault, Value = valueDefault });
Type enumType = typeof(T);
if (!enumType.IsEnum)
{
return list;
}
foreach (var item in Enum.GetNames(enumType))
{
var field = enumType.GetField(item);
string description = item;
object[] arr = field.GetCustomAttributes(typeof(DescriptionAttribute), true); //获取属性字段数组
if (arr != null && arr.Length > 0)
{
description = ((DescriptionAttribute)arr[0]).Description; //属性描述
}
list.Add(new SelectListItem() { Text = description, Value = ((int)Enum.Parse(enumType, item)).ToString() });
}
return list;
}