.net MVC 中枚举类型Enum 转化成 下拉列表的数据源
第一次写技术博文,记录下工作中遇到的问题,给自己的知识做个备份,也希望能帮助到其他的同学
最近接手了公司的一个新的项目。有个页面涉及相关设计。 分享一个经常用到的吧。
方法一:
直入主题吧
我们的目的是把 Enum类型里面的
Enum:
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace AirExpress.Core
{
public enum PackageExceptionTypeEnums
{
[Display(Name = "破损")]
Damaged = , [Display(Name = "违禁品")]
ContrabandGoods = , [Display(Name = "超件")]
BeyondPackages =
}
}
实现效果:

拓展类:
public static class EnumExtention
{ public static SelectList ToSelectList<T>(int? selectValue = null,string AttributeName ="Name")
{
IList<SelectListItem> selectItemList = new List<SelectListItem>();
Type enumType = typeof(T);
Type attrType = typeof(DisplayAttribute);
foreach (int value in Enum.GetValues(typeof(T)))
{
SelectListItem item = new SelectListItem(); string name = Enum.GetName(enumType, value);
object[] objAttrs = enumType.GetField(name).GetCustomAttributes(attrType, true);
if(objAttrs!=null && objAttrs[] is DisplayAttribute)
{
DisplayAttribute descAttr = objAttrs[] as DisplayAttribute;
PropertyInfo propertyName = attrType.GetProperty(AttributeName);
item.Text = propertyName.GetValue(descAttr).ToString();
}else
{
item.Text = Enum.GetName(enumType, value);
} item.Value = value.ToString();
item.Selected = value == selectValue ? true : false;
selectItemList.Add(item);
} SelectList selectList = new SelectList(selectItemList, "Value", "Text", selectValue.Value);
return selectList;
}
}
Attribute 类
public class DisplayAttribute : Attribute
{ public string Name { get; set; }
public string FullName { get; set; } }
Controller:
ViewBag.IndexexceptionSelectedList = EnumsExtension.ToSelectList<PackageExceptionTypeEnums>(input.ExceptionType);
View:
@Html.DropDownListFor(m => m.ExceptionType, ViewBag.exceptionSelectedList as SelectList, "----请选择----")
方法二:
。net MVC 里其实也自带了相应的 下拉列表拓展
EnumDropDownListFor
后来我找资料的时候无意中看到了。 那就不用自己再去实现一遍了
让我们先看下MVC自带的源码文件:
public static class SelectExtensions
{ public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name); public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList); public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name, string optionLabel); public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, IDictionary<string, object> htmlAttributes); public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, object htmlAttributes); public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, string optionLabel); public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, string optionLabel, IDictionary<string, object> htmlAttributes); public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, string optionLabel, object htmlAttributes); public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList); public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, IDictionary<string, object> htmlAttributes); public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, object htmlAttributes); public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string optionLabel); public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string optionLabel, IDictionary<string, object> htmlAttributes); public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string optionLabel, object htmlAttributes); public static MvcHtmlString EnumDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression); public static MvcHtmlString EnumDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression, IDictionary<string, object> htmlAttributes); public static MvcHtmlString EnumDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression, object htmlAttributes); public static MvcHtmlString EnumDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression, string optionLabel); public static MvcHtmlString EnumDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression, string optionLabel, IDictionary<string, object> htmlAttributes); public static MvcHtmlString EnumDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression, string optionLabel, object htmlAttributes); public static MvcHtmlString ListBox(this HtmlHelper htmlHelper, string name); public static MvcHtmlString ListBox(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList); public static MvcHtmlString ListBox(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, IDictionary<string, object> htmlAttributes); public static MvcHtmlString ListBox(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, object htmlAttributes); public static MvcHtmlString ListBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList); public static MvcHtmlString ListBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, IDictionary<string, object> htmlAttributes); public static MvcHtmlString ListBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, object htmlAttributes);
}
view:
//x.TakeoOffPeriod 是枚举类型
@Html.EnumDropDownListFor(x => x.TakeoffPeriod, new AirExpress.Core.TakeoffPeriod())
Enum:
public enum TakeoffPeriod
{
[Display(Name = "全天")]
Allday = ,
[Display(Name = "早班6:00-10:00")]
Morning = ,
[Display(Name = "午班10:00-18:00")]
Noon = ,
[Display(Name = "晚班18:00-次日6:00")]
Night =
}

.net MVC 中枚举类型Enum 转化成 下拉列表的数据源的更多相关文章
- 《挑战30天C++入门极限》新手入门:C/C++中枚举类型(enum)
新手入门:C/C++中枚举类型(enum) 如果一个变量你需要几种可能存在的值,那么就可以被定义成为枚举类型.之所以叫枚举就是说将变量或者叫对象可能存在的情况也可以说是可能的值一一例举出来. ...
- MVC中将枚举类型数据应用到下拉列表中的方法
例如: public enum ItemTypes { Movie = 1, Game = 2, Book = 3 } 在MVC2.0中如何将以上枚举类型使 ...
- Java中枚举类型Enum的一种使用方式
枚举类定义如下: public enum Status { SCUUESS("1", "成功"), FAILED("2", "失败 ...
- C# 中的枚举类型 enum (属于值类型)
原文 C# 中的枚举类型 enum (属于值类型) C# 支持两种特殊的值类型:枚举和结构. 声明枚举:声明时要声明所有可能的值. using System; using System.Collect ...
- 全面解读Java中的枚举类型enum的使用
这篇文章主要介绍了Java中的枚举类型enum的使用,开始之前先讲解了枚举的用处,然后还举了枚举在操作数据库时的实例,需要的朋友可以参考下 关于枚举 大多数地方写的枚举都是给一个枚举然后例子就开始sw ...
- [转载] Java中枚举类型的使用 - enum
目录 1 枚举类的编译特性 2 向枚举类中添加方法 3 接口内部创建枚举 4 枚举类中使用枚举 5 扩展: 验证values()不是通过父类继承的 本文转载自博客 - Java枚举类型, 博主对原文内 ...
- 枚举类型enum详解——C语言
enum enum是C语言中的一个关键字,enum叫枚举数据类型,枚举数据类型描述的是一组整型值的集合(这句话其实不太妥当),枚举型是预处理指令#define的替代,枚举和宏其实非常类似,宏在预处理阶 ...
- 人生苦短之Python枚举类型enum
枚举类型enum是比较重要的一个数据类型,它是一种数据类型而不是数据结构,我们通常将一组常用的常数声明成枚举类型方便后续的使用.当一个变量有几种可能的取值的时候,我们将它定义为枚举类型.在Python ...
- 【转】java枚举类型enum的使用
原文网址:http://blog.csdn.net/wgw335363240/article/details/6359614 java 枚举类型enum 的使用 最近跟同事讨论问题的时候,突然同事提到 ...
随机推荐
- Javascript模式(第一章简介)------读书笔记
一:模式 模式是一个通用问题的解决方案,可以提供一个更好的实践经验.有用的抽象化表示和解决一类问题的模板. 本书主要讨论如下三种类型的模式 1 设计模式:可复用面向对象软件的基础,包括singleto ...
- ionic 启用sass
转入ionic项目目录,命令行下执行:ionic setup sass 提示编译器未装: You have specified Ionic CI to set up sass.However, you ...
- eclipse使用技巧之 //TODO标识
通常有三种方式去表示你的待办: //TODO 待实现 //XXX 勉强可以工作,但是性能差 //FIXME 代码错误,必须修复. 在task窗口可以查找所有TODO. 使用ctrl + K 去单页面定 ...
- Jenkins若干小问题
1. Jenkins上不能直接在shell中调用scp命令来执行上传下载操作,核心问题是scp需要输入密码. 为了可以直接将密码传递过去.我们安装 sshpass 来透传密码 a. 安装sshpas ...
- ASP.NET管道
以IIS 6.0为例,在工作进程w3wp.exe中,利用Aspnet_ispai.dll加载.NET运行时(如果.NET运行时尚未加载).IIS 6引入了应用程序池的概念,一个工作进程对应着一个应用程 ...
- 学习indy组件之一idhttp的使用方法
登录 注册 百度首页 新闻 网页 贴吧 知道 音乐 图片 视频 地图 百科 文库 经验 搜索答案我要提问 首页 分类 公社 知道行家 问医生 高质量问答 经验 个人中心手机知道开放平台 关于del ...
- php删除目录下的所有文件和目录
<?php /** * 递归实现删除目录下的所有的文件和文件夹 * @param $dir 要删除的目录 * @param bool $deleteRootToo 是否删除根目录 默认不删除 h ...
- 使用VS2013逆向生成UML类图
引自http://blog.csdn.net/funnyfu0101/article/details/7705173 首先.打开工程,[体系结构]->[新建关系图] 生成一个类图 然后[体系结构 ...
- verilog断言(SVA:systemverlog assertion)语法 ---- 转载
转载自:http://blog.sina.com.cn/s/blog_4c270c730101f6mw.html 作者:白栎旸 断言assertion被放在verilog设计中,方便在仿真时查 ...
- 上海敏行医学招聘物理仿真,3D图形人才
工作职能: 1.开发医学虚拟手术中的柔体仿真引擎/图形效果 2.柔体仿真引擎.和引擎开发主工程师一起完善和改进仿真引擎的开发工作. 3.3D图形效果的改进. 职位要求: 1.本科以上学历,1年以上c+ ...