/// <summary>
/// 将enum转换成List<Model.Models.SelectViewModels>,即html的select标签使用的数据
/// </summary>
/// <returns></returns>
public static List<Model.Models.SelectViewModels> EnumToSelect(Type t)
{
List<Model.Models.SelectViewModels> select = new List<Model.Models.SelectViewModels>();
foreach (var e in Enum.GetValues(t))
{
Model.Models.SelectViewModels model = new Model.Models.SelectViewModels();
model.Text = e.ToString();
model.Value = Enum.Format(t, e, "d");
select.Add(model);
}
return select;
}
使用属性器获得属性值
public enum RedPackStatus
{
[Description("发放中")]
SENDING,
[Description("已发放待领取")]
SENT,
[Description("发放失败")]
FAILED,
[Description("已领取")]
RECEIVED,
[Description("退款中")]
RFUND_ING,
[Description("已退款")]
REFUND, } /// <summary>
/// 获取枚举描述信息
/// </summary>
/// <param name="enumType">枚举类型</param>
/// <param name="enumStr">枚举字值字符串</param>
/// <returns></returns>
public static string GetEnumDescription(Type enumType, string enumStr)
{
FieldInfo p = enumType.GetField(enumStr);
object[] os = p.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), true);
if (os == null || os.Length == )
{
return null;
}
return ((DescriptionAttribute)os[]).Description; } public ActionResult Index(string id)
{
string a = Common.EnumToList.GetEnumDescription(typeof(Utility.Enums.WeiXin.RedPackStatus), "SENDING");
return Content("a:"+a);
}