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

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

方法一:

直入主题吧

我们的目的是把 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. Keepalived+Nginx架构整理版

    Keepalived介绍 keepalived是一个类似于layer3, 4, 5 交换机制的软件,也就是我们平时说的第3层.第4层和第5层交换.Keepalived的作用是检测web服务器的状态,如 ...

  2. 基于Lattice_CPLD/FPGA Diamond 开发流程

         本文主要介绍了Lattice CPLD/FPGA集成开发环境的使用方法,并通过点亮开发板(Mach XO2 Breakout Board)上位号为D2的LED这一实例来演示其开发流程. 1. ...

  3. asp.net教程:GridView导出到Excel或Word文件

    asp.net教程:GridView导出到Excel或Word文件</ br> 在项目中我们经常会遇到要求将一些数据导出成Excel或者Word表格的情况,比如中国移动(我是中国移动用户) ...

  4. Linux centos 下 安装eclipse c++

    之前在centos6.3版本使用eclipes一切都很正常.最近centos版本升级到6.7后,使用eclipse c++到时候,打开文件,就异常退出了.在网上搜了很久,终于找到解决方法: 现象描述: ...

  5. [platform]Device和Driver注册顺序

    1. 设备和驱动注册,无论谁先谁后,都可以通过查询总线进行匹配 设备挂接到总线上时,与总线上的所有驱动进行匹配(用bus_type.match进行匹配),如果匹配成功,则调用bus_type.prob ...

  6. 擦掉STM32F429芯片上的数据的一个方法

    刚入手一块STM32F429Discovery.手痒痒的,准备写个程序进去.一不小心,把MCU的调试接口SW.JTAG全部给禁用了.这下可坏了,写不进去程序,擦不掉数据.愁的某家一头大汗.突然想起了当 ...

  7. 黄聪:日租VPS中FileZilla_Server配置方法

    1.关闭VPS中IIS的FTP服务 2.FileZilla_Server 监听端口 21 3.FTP客户端端口为11311(看服务商给出的)

  8. 关于PetaPoco的T4模板使用

    PetaPoco是一款适用于.Net 和Mono的微小.快速.单文件的微型ORM.PetaPoco介绍:http://www.cnblogs.com/youring2/archive/2012/06/ ...

  9. unity 读取文本与写入文本

    void writeData(string str,string file)    {        string parth = Application.dataPath;        Strea ...

  10. 思考方式--SMART原则

    如果你对项目管理.系统架构有兴趣,请加微信订阅号"softjg",加入这个PM.架构师的大家庭 万事开头于你目标的设定,如果开始走错了,那么后面的路将会更加的错误,甚至于更加的努力 ...