第一次写技术博文,记录下工作中遇到的问题,给自己的知识做个备份,也希望能帮助到其他的同学

最近接手了公司的一个新的项目。有个页面涉及相关设计。 分享一个经常用到的吧。

方法一:

直入主题吧

我们的目的是把 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 转化成 下拉列表的数据源的更多相关文章

  1. 《挑战30天C++入门极限》新手入门:C/C++中枚举类型(enum)

        新手入门:C/C++中枚举类型(enum) 如果一个变量你需要几种可能存在的值,那么就可以被定义成为枚举类型.之所以叫枚举就是说将变量或者叫对象可能存在的情况也可以说是可能的值一一例举出来. ...

  2. MVC中将枚举类型数据应用到下拉列表中的方法

    例如: public enum ItemTypes   {      Movie = 1,      Game = 2,      Book = 3   }    在MVC2.0中如何将以上枚举类型使 ...

  3. Java中枚举类型Enum的一种使用方式

    枚举类定义如下: public enum Status { SCUUESS("1", "成功"), FAILED("2", "失败 ...

  4. C# 中的枚举类型 enum (属于值类型)

    原文 C# 中的枚举类型 enum (属于值类型) C# 支持两种特殊的值类型:枚举和结构. 声明枚举:声明时要声明所有可能的值. using System; using System.Collect ...

  5. 全面解读Java中的枚举类型enum的使用

    这篇文章主要介绍了Java中的枚举类型enum的使用,开始之前先讲解了枚举的用处,然后还举了枚举在操作数据库时的实例,需要的朋友可以参考下 关于枚举 大多数地方写的枚举都是给一个枚举然后例子就开始sw ...

  6. [转载] Java中枚举类型的使用 - enum

    目录 1 枚举类的编译特性 2 向枚举类中添加方法 3 接口内部创建枚举 4 枚举类中使用枚举 5 扩展: 验证values()不是通过父类继承的 本文转载自博客 - Java枚举类型, 博主对原文内 ...

  7. 枚举类型enum详解——C语言

    enum enum是C语言中的一个关键字,enum叫枚举数据类型,枚举数据类型描述的是一组整型值的集合(这句话其实不太妥当),枚举型是预处理指令#define的替代,枚举和宏其实非常类似,宏在预处理阶 ...

  8. 人生苦短之Python枚举类型enum

    枚举类型enum是比较重要的一个数据类型,它是一种数据类型而不是数据结构,我们通常将一组常用的常数声明成枚举类型方便后续的使用.当一个变量有几种可能的取值的时候,我们将它定义为枚举类型.在Python ...

  9. 【转】java枚举类型enum的使用

    原文网址:http://blog.csdn.net/wgw335363240/article/details/6359614 java 枚举类型enum 的使用 最近跟同事讨论问题的时候,突然同事提到 ...

随机推荐

  1. putty+xming远程登录Ubuntu16.04图形界面

    前面我写过一篇<Ubuntu16.04 安装ftp服务器传输文件>的文章.文章当中已经可以远程访问linux服务器并且传输文件,然而要在putty中使用开启图形界面的命令到目前为止还是不够 ...

  2. Java基础--serialVersionUID

    Java基础--serialVersionUID serialVersionUID作用: 序列化时为了保持版本的兼容性,即在版本升级时反序列化仍保持对象的唯一性.有两种生成方式: 一个是默认的1L,比 ...

  3. [Linux] - Docker移动数据到其它盘的办法

    由于使用yum安装Docker,默认是数据是存放在系统盘/var/lib目录下,需要把它放到其实盘里头.方法可以这样做: 1.在其它盘中新建一个目录,比如我的:/yunpan/docker mkdir ...

  4. cPage分页源码,分享给大家,可作参考

    cPage是asp.net分页控件,也可以叫做分页组件,更确切的应该叫做分页模块,也或者叫做分页通用代码. cPage,版本3.2,源码如下: using System; namespace cPag ...

  5. 使用PowerDesigner把oom设计图导出jpg格式的图片

    1: 按住Shift键点击鼠标选择要导出的对象,必须先选择. 2: 选择Edit—>Export Image 到出你需要的格式,如下图

  6. cookie禁用了,session还能用吗?

    Cookie与 Session,一般认为是两个独立的东西,Session采用的是在服务器端保持状态的方案,而Cookie采用的是在客户端保持状态的方案.但为什么禁用Cookie就不能得到Session ...

  7. mysql:权限分配

    grant all privileges on *.* to  name@localhost identified by '1'; flush privileges;

  8. Spark SQL Example

     Spark SQL Example This example demonstrates how to use sqlContext.sql to create and load a table ...

  9. spring 和 spring mvc

    spring3 http://jinnianshilongnian.iteye.com/blog/1482071 spring mvc http://jinnianshilongnian.iteye. ...

  10. 下载Tomcat时Tomcat网站上的core和deployer的区别

    下载Tomcat时Tomcat网站上的core和deployer的区别 做JavaEE开发的朋友,无论是学习者还是已经工作的朋友,总是会用到Tomcat这个Servlet容器,那么大家从Tomcat官 ...