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

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

方法一:

直入主题吧

我们的目的是把 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. vector与list区别

    vector为存储的对象分配一块连续的地址空间,因此对vector中的元素随机访问效率很高.在vecotor中插入或者删除某个元素,需要将现有元素进行复制,移动.如果vector中存储的对象很大,或者 ...

  2. css3 em rem等单位的区别

    px:绝对单位,页面按精确像素展示 em:相对单位,基准点为父节点字体的大小,如果自身定义了font-size按自身来计算(浏览器默认字体是16px),整个页面内1em不是一个固定的值. rem:相对 ...

  3. 【Infobright】infobright数据导入导出测试

    创建数据库 create database if not exists `mytestdb` default charset=utf8; use mytestdb; 说明: 如果使用utf8字符集,则 ...

  4. <<面向模式的软件架构2-并发和联网对象模式>>读书笔记

    服务访问和配置模式 Wrapper Facade可以将有非对象API提供的函数和数据封装到面向对象的类接口中 就是把底层API再封装一次,让外部不用关心是调用哪个平台的API,不如锁,在不同的平台上可 ...

  5. openssl创建非认证的https证书(红色的)

    1.生成csr,key openssl req -new -newkey rsa:2048 -sha256 -nodes -out example_com.csr -keyout example_co ...

  6. android studio 各种问题

    1.dexDebug ExecException finished with non-zero exit value 2 全bug日志如下: (Error:Execution failed for t ...

  7. es6 class

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  8. 解决类似 Requires: libstdc++.so.6(GLIBCXX_3.4.15)(64bit)的问题

    源码编译升级安装了gcc后,编译程序或运行其它程序时,有时会出现类似/usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found的问题.这 ...

  9. python中的单引号,双引号,三引号

    转载自: http://blog.csdn.net/wanghai__/article/details/6285310 先说1双引号与3个双引号的区别,双引号所表示的字符串通常要写成一行 如: s1 ...

  10. install cpanm

    wget http://cpanmin.us mv index.html cpanm chmod +x cpanm