---帮助类
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
namespace WebApplication5
{
public class EnumHelper
{
/// <summary>
/// 枚举转字典集合
/// </summary>
/// <typeparam name="T">枚举类名称</typeparam>
/// <param name="keyDefault">默认key值</param>
/// <param name="valueDefault">默认value值</param>
/// <returns>返回生成的字典集合</returns>
public static Dictionary<string, object> EnumListDic<T>(string keyDefault, string valueDefault = "")
{
Dictionary<string, object> dicEnum = new Dictionary<string, object>();
Type enumType = typeof(T);
if (!enumType.IsEnum)
{
return dicEnum;
}
if (!string.IsNullOrEmpty(keyDefault)) //判断是否添加默认选项
{
dicEnum.Add(keyDefault, valueDefault);
}
string[] fieldstrs = Enum.GetNames(enumType); //获取枚举字段数组
foreach (var item in fieldstrs)
{
string description = string.Empty;
var field = enumType.GetField(item);
object[] arr = field.GetCustomAttributes(typeof(DescriptionAttribute), true); //获取属性字段数组
if (arr != null && arr.Length > 0)
{
description = ((DescriptionAttribute)arr[0]).Description; //属性描述
}
else
{
description = item; //描述不存在取字段名称
}
dicEnum.Add(description, (int)Enum.Parse(enumType, item)); //不用枚举的value值作为字典key值的原因从枚举例子能看出来,其实这边应该判断他的值不存在,默认取字段名称
}
return dicEnum;
}
}
}
---控制器
public ActionResult Enums()
{
Dictionary<string, object> Droplist = EnumHelper.EnumListDic<Project_Emun>("请选择", "-1");
//C#MVC的方便之处也在这里,List 集合要与下拉框进行绑定可以直接绑定 ViewBag.dropList = new SelectList(list,"对象字段1","对象字段2","默认选择");
ViewBag.Drop = new SelectList(Droplist, "value", "key");
return View();
}
--枚举类
public enum Project_Emun
{
[Description("开始")]
开始 = 1,
[Description("中断")]
中断 = 2,
[Description("结束")]
结束 = 3,
[Description("回馈")]
回馈 = 4,
}