加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

[asp.net mvc 奇淫巧技] 03 - 枚举特性扩展解决枚举命名问题和支

发布时间:2020-12-15 21:23:49 所属栏目:asp.Net 来源:网络整理
导读:一、需求 我们在开发中经常会遇到一些枚举,而且这些枚举类型可能会在表单中的下拉中,或者单选按钮中会用到等。 这样用是没问题的,但是用过的人都知道一个问题,就是枚举的 命名问题 ,当然有很多人枚举直接中文命名,我是不推荐这种命名规则,因为实在不

一、需求

我们在开发中经常会遇到一些枚举,而且这些枚举类型可能会在表单中的下拉中,或者单选按钮中会用到等。

这样用是没问题的,但是用过的人都知道一个问题,就是枚举的命名问题,当然有很多人枚举直接中文命名,我是不推荐这种命名规则,因为实在不够友好。

那有没有可以不用中文命名,而且可以显示中文的方法呢。答案是肯定的。

二、特性解决枚举命名问题

那就是用特性解决命名问题,这样的话既可以枚举用英文命名,显示又可以是中文的,岂不两全其美。

[Description(= [Description(= [Description(= [Description(=

?

1、新建枚举的特性类

首先我们需要新建枚举的特性,用来描述枚举,这样既可以解决枚举的命名问题,又可以解决枚举的显示问题。

我们在下拉框或者单选按钮上显示各个枚举项,可能会出现一些排序问题,所以在枚举的特性上不仅有显示的名称还有排序。

[AttributeUsage(AttributeTargets.Field,AllowMultiple = ,Inherited = Order { ; </span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<summary></span> <span style="color: #808080;"&gt;///</span><span style="color: #008000;"&gt; 名称 </span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;</summary></span> <span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;string</span> Name { <span style="color: #0000ff;"&gt;get</span>; <span style="color: #0000ff;"&gt;set</span><span style="color: #000000;"&gt;; } </span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<summary></span> <span style="color: #808080;"&gt;///</span><span style="color: #008000;"&gt; 显示自定义描述名称 </span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;</summary></span> <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<param name="name"&gt;</span><span style="color: #008000;"&gt;名称</span><span style="color: #808080;"&gt;</param></span> <span style="color: #0000ff;"&gt;public</span> DescriptionAttribute(<span style="color: #0000ff;"&gt;string</span><span style="color: #000000;"&gt; name) { Name </span>=<span style="color: #000000;"&gt; name; } </span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<summary></span> <span style="color: #808080;"&gt;///</span><span style="color: #008000;"&gt; 显示自定义名称 </span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;</summary></span> <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<param name="name"&gt;</span><span style="color: #008000;"&gt;名称</span><span style="color: #808080;"&gt;</param></span> <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<param name="order"&gt;</span><span style="color: #008000;"&gt;排序</span><span style="color: #808080;"&gt;</param></span> <span style="color: #0000ff;"&gt;public</span> DescriptionAttribute(<span style="color: #0000ff;"&gt;string</span> name,<span style="color: #0000ff;"&gt;int</span><span style="color: #000000;"&gt; order) { Name </span>=<span style="color: #000000;"&gt; name; Order </span>=<span style="color: #000000;"&gt; order; } }</span></pre>

?新建好枚举的特性类以后,我们就可以在枚举的字段上添加自定义的特性Description

[Description(,= [Description(,= [Description(,= [Description(,=

特性第一个参数为名称,第二个为排序(int 类型,正序),这就是就是我们新建枚举时在需要显示和枚举名称不一样的枚举字段上添加即可。这个Gender枚举,在后面文章中会一直用到(Gender)。

2、新建枚举扩展方法获取枚举特性的描述

我们前面的工作已经把特性和在枚举上添加特性已经完成了,后面我们需要的就是要获取我们添加的描述和排序。

</span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<summary></span> <span style="color: #808080;"&gt;///</span><span style="color: #008000;"&gt; 获取当前枚举值的描述 </span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;</summary></span> <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<param name="value"&gt;</param></span> <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<returns></returns></span> <span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;static</span> <span style="color: #0000ff;"&gt;string</span> GetDescription(<span style="color: #0000ff;"&gt;this</span><span style="color: #000000;"&gt; Enum value) { </span><span style="color: #0000ff;"&gt;int</span><span style="color: #000000;"&gt; order; </span><span style="color: #0000ff;"&gt;return</span> GetDescription(value,<span style="color: #0000ff;"&gt;out</span><span style="color: #000000;"&gt; order); } </span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<summary></span> <span style="color: #808080;"&gt;///</span><span style="color: #008000;"&gt; 获取当前枚举值的描述和排序 </span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;</summary></span> <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<param name="value"&gt;</param></span> <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<param name="order"&gt;</param></span> <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<returns></returns></span> <span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;static</span> <span style="color: #0000ff;"&gt;string</span> GetDescription(<span style="color: #0000ff;"&gt;this</span> Enum value,<span style="color: #0000ff;"&gt;out</span> <span style="color: #0000ff;"&gt;int</span><span style="color: #000000;"&gt; order) { </span><span style="color: #0000ff;"&gt;string</span> description = <span style="color: #0000ff;"&gt;string</span><span style="color: #000000;"&gt;.Empty; Type type </span>=<span style="color: #000000;"&gt; value.GetType(); </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 获取枚举</span> FieldInfo fieldInfo =<span style="color: #000000;"&gt; type.GetField(value.ToString()); </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 获取枚举自定义的特性DescriptionAttribute</span> <span style="color: #0000ff;"&gt;object</span>[] attrs = fieldInfo.GetCustomAttributes(<span style="color: #0000ff;"&gt;typeof</span>(DescriptionAttribute),<span style="color: #0000ff;"&gt;false</span><span style="color: #000000;"&gt;); DescriptionAttribute attr </span>= (DescriptionAttribute)attrs.FirstOrDefault(a => a <span style="color: #0000ff;"&gt;is</span><span style="color: #000000;"&gt; DescriptionAttribute); order </span>= <span style="color: #800080;"&gt;0</span><span style="color: #000000;"&gt;; description </span>=<span style="color: #000000;"&gt; fieldInfo.Name; </span><span style="color: #0000ff;"&gt;if</span> (attr != <span style="color: #0000ff;"&gt;null</span><span style="color: #000000;"&gt;) { order </span>=<span style="color: #000000;"&gt; attr.Order; description </span>=<span style="color: #000000;"&gt; attr.Name; } </span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; description; }

}

3、获取枚举描述和排序

至此:我们可以很容易获取到枚举添加的特性描述和排序。

des =

<span style="color: #0000ff;">var name =<span style="color: #000000;"> Gender.Male.ToString();
<span style="color: #008000;">//<span style="color: #008000;"> name= "Male"

<span style="color: #0000ff;">var key = (<span style="color: #0000ff;">int<span style="color: #000000;">)Gender.Male;
<span style="color: #008000;">//<span style="color: #008000;"> key = 2

<span style="color: #0000ff;">int<span style="color: #000000;"> order;
<span style="color: #0000ff;">var des1 = Gender.Female.GetDescription(<span style="color: #0000ff;">out<span style="color: #000000;"> order);
<span style="color: #008000;">//<span style="color: #008000;"> des1 = “女性”,order= 2

这样我们就很好的解决了枚举命名问题, 可以很容易的获取到枚举的描述信息,也就是要显示的信息。但是我们需要的是一次性可以查询全部的枚举信息,以便我们进行显示。

三、获取所有枚举的描述和值,以便循环使用

我们已经可以很容易的获取到枚举的值,名称和描述了,所以后面的就很简单了。

</span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<summary></span> <span style="color: #808080;"&gt;///</span><span style="color: #008000;"&gt; 获取当前枚举的所有描述 </span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;</summary></span> <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<param name="value"&gt;</param></span> <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<returns></returns></span> <span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;static</span> List<KeyValuePair<<span style="color: #0000ff;"&gt;int</span>,<span style="color: #0000ff;"&gt;string</span>>> GetAll<T><span style="color: #000000;"&gt;() { </span><span style="color: #0000ff;"&gt;return</span> GetAll(<span style="color: #0000ff;"&gt;typeof</span><span style="color: #000000;"&gt;(T)); } </span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<summary></span> <span style="color: #808080;"&gt;///</span><span style="color: #008000;"&gt; 获取所有的枚举描述和值 </span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;</summary></span> <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<param name="type"&gt;</param></span> <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<returns></returns></span> <span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;static</span> List<KeyValuePair<<span style="color: #0000ff;"&gt;int</span>,<span style="color: #0000ff;"&gt;string</span>>><span style="color: #000000;"&gt; GetAll(Type type) { List</span><EnumToolsModel> list = <span style="color: #0000ff;"&gt;new</span> List<EnumToolsModel><span style="color: #000000;"&gt;(); </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 循环枚举获取所有的Fields</span> <span style="color: #0000ff;"&gt;foreach</span> (<span style="color: #0000ff;"&gt;var</span> field <span style="color: #0000ff;"&gt;in</span><span style="color: #000000;"&gt; type.GetFields()) { </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 如果是枚举类型</span> <span style="color: #0000ff;"&gt;if</span><span style="color: #000000;"&gt; (field.FieldType.IsEnum) { </span><span style="color: #0000ff;"&gt;object</span> tmp = field.GetValue(<span style="color: #0000ff;"&gt;null</span><span style="color: #000000;"&gt;); Enum enumValue </span>=<span style="color: #000000;"&gt; (Enum)tmp; </span><span style="color: #0000ff;"&gt;int</span> intValue =<span style="color: #000000;"&gt; Convert.ToInt32(enumValue); </span><span style="color: #0000ff;"&gt;int</span><span style="color: #000000;"&gt; order; </span><span style="color: #0000ff;"&gt;string</span> showName = enumValue.GetDescription(<span style="color: #0000ff;"&gt;out</span> order); <span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 获取描述和排序</span> list.Add(<span style="color: #0000ff;"&gt;new</span> EnumToolsModel { Key = intValue,Name = showName,Order =<span style="color: #000000;"&gt; order }); } } </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 排序并转成KeyValue返回</span> <span style="color: #0000ff;"&gt;return</span> list.OrderBy(i => i.Order).Select(i => <span style="color: #0000ff;"&gt;new</span> KeyValuePair<<span style="color: #0000ff;"&gt;int</span>,<span style="color: #0000ff;"&gt;string</span>><span style="color: #000000;"&gt;(i.Key,i.Name)).ToList(); }

}

调用:这样我们就很容易的获取枚举所有字段的描述,如我们需要在cshtml中调用

(Typeof(Gender)) @foreach (var item in genders) {

生成的html为:

男性 女性 未知 人妖

?这样我们就已顺利的解决了枚举的命名以及排序显示等问题。

四、枚举特性扩展至HtmlHelper

我们已经解决了枚举的命名以及排序显示问题,但是我们想做的更好,比如每次都要写一个foreach获取所有的枚举然后在判断默认值和哪个相等,循环遍历,周而复始,重复造轮子,bad code。所以我们要进行封装,封装成与?@Html.DropDownList一样好用的HtmlHelper扩展。

MvcHtmlString EnumToolsSelect( HtmlHelper html, htmlAttributes = html.EnumToolsSelect((T),<span style="color: #808080;">/// <span style="color: #808080;">
<span style="color: #808080;">///<span style="color: #008000;"> 枚举下拉
<span style="color: #808080;">/// <span style="color: #808080;">

<span style="color: #808080;">///
<span style="color: #808080;"><typeparam name="T">
<span style="color: #008000;">枚举类型
<span style="color: #808080;">

<span style="color: #808080;">///
<span style="color: #808080;"><param name="html">

<span style="color: #808080;">///
<span style="color: #808080;"><param name="selectedValue">
<span style="color: #008000;">选择项
<span style="color: #808080;">

<span style="color: #808080;">///
<span style="color: #808080;"><param name="htmlAttributes">

<span style="color: #808080;">///
<span style="color: #808080;">

<span style="color: #0000ff;">public
<span style="color: #0000ff;">static
MvcHtmlString EnumToolsSelect(<span style="color: #0000ff;">this
HtmlHelper html,<span style="color: #0000ff;">int selectedValue,<span style="color: #0000ff;">object htmlAttributes = <span style="color: #0000ff;">null<span style="color: #000000;">)
{
<span style="color: #0000ff;">return html.EnumToolsSelect(<span style="color: #0000ff;">typeof<span style="color: #000000;">(T),selectedValue,htmlAttributes);
}

<span style="color: #808080;">/// <span style="color: #808080;">


<span style="color: #808080;">///<span style="color: #008000;"> 枚举下拉
<span style="color: #808080;">/// <span style="color: #808080;">

<span style="color: #808080;">/// <span style="color: #808080;"><typeparam name="T"><span style="color: #008000;">枚举类型<span style="color: #808080;">
<span style="color: #808080;">/// <span style="color: #808080;"><param name="html">
<span style="color: #808080;">/// <span style="color: #808080;"><param name="selectedValue"><span style="color: #008000;">选择项<span style="color: #808080;">
<span style="color: #808080;">/// <span style="color: #808080;"><param name="htmlAttributes">
<span style="color: #808080;">/// <span style="color: #808080;">
<span style="color: #0000ff;">public <span style="color: #0000ff;">static MvcHtmlString EnumToolsSelect(<span style="color: #0000ff;">this HtmlHelper html,T selectedValue,Convert.ToInt32(selectedValue),htmlAttributes);
}

<span style="color: #808080;">/// <span style="color: #808080;">


<span style="color: #808080;">///<span style="color: #008000;"> 枚举下拉
<span style="color: #808080;">/// <span style="color: #808080;">

<span style="color: #808080;">/// <span style="color: #808080;"><param name="html">
<span style="color: #808080;">/// <span style="color: #808080;"><param name="enumType"><span style="color: #008000;">枚举类型<span style="color: #808080;">
<span style="color: #808080;">/// <span style="color: #808080;"><param name="selectedValue"><span style="color: #008000;">选择项<span style="color: #808080;">
<span style="color: #808080;">/// <span style="color: #808080;"><param name="htmlAttributes">
<span style="color: #808080;">/// <span style="color: #808080;">
<span style="color: #0000ff;">public <span style="color: #0000ff;">static MvcHtmlString EnumToolsSelect(<span style="color: #0000ff;">this HtmlHelper html,Type enumType,<span style="color: #0000ff;">object htmlAttributes = <span style="color: #0000ff;">null<span style="color: #000000;">)
{
<span style="color: #008000;">//<span style="color: #008000;"> 创建标签
TagBuilder tag = <span style="color: #0000ff;">new TagBuilder(<span style="color: #800000;">"<span style="color: #800000;">select<span style="color: #800000;">"<span style="color: #000000;">);

</span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 添加自定义标签</span>
<span style="color: #0000ff;"&gt;if</span> (htmlAttributes != <span style="color: #0000ff;"&gt;null</span><span style="color: #000000;"&gt;)
{
    RouteValueDictionary htmlAttr </span>= htmlAttributes <span style="color: #0000ff;"&gt;as</span> RouteValueDictionary ??<span style="color: #000000;"&gt; HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
    tag.MergeAttributes(htmlAttr);
}
</span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 创建option集合</span>
StringBuilder options = <span style="color: #0000ff;"&gt;new</span><span style="color: #000000;"&gt; StringBuilder();
</span><span style="color: #0000ff;"&gt;foreach</span> (<span style="color: #0000ff;"&gt;var</span> item <span style="color: #0000ff;"&gt;in</span><span style="color: #000000;"&gt; GetAll(enumType))
{
    </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 创建option</span>
    TagBuilder option = <span style="color: #0000ff;"&gt;new</span> TagBuilder(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;option</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;);

    </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 添加值</span>
    option.MergeAttribute(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;value</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;,item.Key.ToString());

    </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 设置选择项</span>
    <span style="color: #0000ff;"&gt;if</span> (item.Key ==<span style="color: #000000;"&gt; selectedValue)
    {
        option.MergeAttribute(</span><span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;selected</span><span style="color: #800000;"&gt;"</span>,<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;selected</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;);
    }

    </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 设置option</span>

<span style="color: #000000;"> option.SetInnerText(item.Value);
options.Append(option.ToString());
}
tag.InnerHtml =<span style="color: #000000;"> options.ToString();

</span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 返回MVCHtmlString</span>
<span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; MvcHtmlString.Create(tag.ToString());

}

?然后调用

@(Html.EnumToolsSelect((Gender.Female, { @class = (Gender),)

这样就可以生成你所需要的下拉框的html,一行代码就可以解决复杂的枚举下拉。

你以为就这样结束了吗,很明显没有,因为不是我风格,我的风格是继续封装。

五、枚举特性扩展至HtmlHelper Model

这个可能有很多不会陌生,因为很多HtmlHelper都有一个For结尾的,如@Html.DropDownListFor等等,那我们也要有For结尾的,要不然都跟不上潮流了。

关于For的一些扩展和没有For的扩展的区别,简单来说带For就是和Model一起用的,如:@Html.TextBoxFor(i => i.Name)

这样就可以更加一步的封装,如Id,name,model的Name值以及验证等等。

话不多说,直接代码

MvcHtmlString EnumToolsSelectFor( HtmlHelper htmlHelper,Expression> expression, htmlAttributes = ModelMetadata modelMetadata =Type enumType </span>=<span style="color: #000000;"&gt; modelMetadata.ModelType; </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 设置id name的属性值</span> <span style="color: #0000ff;"&gt;var</span> rvd = <span style="color: #0000ff;"&gt;new</span><span style="color: #000000;"&gt; RouteValueDictionary { { </span><span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;id</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;,modelMetadata.PropertyName },{ </span><span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;name</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;,modelMetadata.PropertyName } }; </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 添加自定义属性</span> <span style="color: #0000ff;"&gt;if</span> (htmlAttributes != <span style="color: #0000ff;"&gt;null</span><span style="color: #000000;"&gt;) { RouteValueDictionary htmlAttr </span>= htmlAttributes <span style="color: #0000ff;"&gt;as</span> RouteValueDictionary ??<span style="color: #000000;"&gt; HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes); </span><span style="color: #0000ff;"&gt;foreach</span> (<span style="color: #0000ff;"&gt;var</span> item <span style="color: #0000ff;"&gt;in</span><span style="color: #000000;"&gt; htmlAttr) { rvd.Add(item.Key,item.Value); } } </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 获取验证信息</span> IDictionary<<span style="color: #0000ff;"&gt;string</span>,<span style="color: #0000ff;"&gt;object</span>> validationAttributes =<span style="color: #000000;"&gt; htmlHelper.GetUnobtrusiveValidationAttributes(modelMetadata.PropertyName,modelMetadata); </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 添加至自定义属性</span> <span style="color: #0000ff;"&gt;if</span> (validationAttributes != <span style="color: #0000ff;"&gt;null</span><span style="color: #000000;"&gt;) { </span><span style="color: #0000ff;"&gt;foreach</span> (<span style="color: #0000ff;"&gt;var</span> item <span style="color: #0000ff;"&gt;in</span><span style="color: #000000;"&gt; validationAttributes) { rvd.Add(item.Key,item.Value); } } </span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; htmlHelper.EnumToolsSelect(enumType,Convert.ToInt32(modelMetadata.Model),rvd);

}

关于使用:

首先我们需要返回view时需要返回Model

View( Person { Age = ,Name = ,Gender =}

<span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> Person
{
<span style="color: #0000ff;">public <span style="color: #0000ff;">string Name { <span style="color: #0000ff;">get; <span style="color: #0000ff;">set<span style="color: #000000;">; }
<span style="color: #0000ff;">public <span style="color: #0000ff;">int Age { <span style="color: #0000ff;">get; <span style="color: #0000ff;">set<span style="color: #000000;">; }
<span style="color: #0000ff;">public Gender Gender { <span style="color: #0000ff;">get; <span style="color: #0000ff;">set<span style="color: #000000;">; }
}

cshtm调用

@Html.EnumToolsSelectFor(i => i.Gender)

生成html代码

男性 女性 未知 人妖

六、全部枚举特性和HtmlHelper代码

<span style="color: #0000ff;">namespace<span style="color: #000000;"> Emrys.EnumTools
{

</span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<summary></span>
<span style="color: #808080;"&gt;///</span><span style="color: #008000;"&gt; 枚举帮助类
</span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;</summary></span>
<span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;static</span> <span style="color: #0000ff;"&gt;class</span><span style="color: #000000;"&gt; EnumTools
{

    </span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<summary></span>
    <span style="color: #808080;"&gt;///</span><span style="color: #008000;"&gt; 获取当前枚举值的描述
    </span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;</summary></span>
    <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<param name="value"&gt;</param></span>
    <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<returns></returns></span>
    <span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;static</span> <span style="color: #0000ff;"&gt;string</span> GetDescription(<span style="color: #0000ff;"&gt;this</span><span style="color: #000000;"&gt; Enum value)
    {
        </span><span style="color: #0000ff;"&gt;int</span><span style="color: #000000;"&gt; order;
        </span><span style="color: #0000ff;"&gt;return</span> GetDescription(value,<span style="color: #0000ff;"&gt;out</span><span style="color: #000000;"&gt; order);
    }

    </span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<summary></span>
    <span style="color: #808080;"&gt;///</span><span style="color: #008000;"&gt; 获取当前枚举值的描述和排序
    </span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;</summary></span>
    <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<param name="value"&gt;</param></span>
    <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<param name="order"&gt;</param></span>
    <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<returns></returns></span>
    <span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;static</span> <span style="color: #0000ff;"&gt;string</span> GetDescription(<span style="color: #0000ff;"&gt;this</span> Enum value,<span style="color: #0000ff;"&gt;out</span> <span style="color: #0000ff;"&gt;int</span><span style="color: #000000;"&gt; order)
    {
        </span><span style="color: #0000ff;"&gt;string</span> description = <span style="color: #0000ff;"&gt;string</span><span style="color: #000000;"&gt;.Empty;

        Type type </span>=<span style="color: #000000;"&gt; value.GetType();

        </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 获取枚举</span>
        FieldInfo fieldInfo =<span style="color: #000000;"&gt; type.GetField(value.ToString());

        </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 获取枚举自定义的特性DescriptionAttribute</span>
        <span style="color: #0000ff;"&gt;object</span>[] attrs = fieldInfo.GetCustomAttributes(<span style="color: #0000ff;"&gt;typeof</span>(DescriptionAttribute),<span style="color: #0000ff;"&gt;false</span><span style="color: #000000;"&gt;);
        DescriptionAttribute attr </span>= (DescriptionAttribute)attrs.FirstOrDefault(a => a <span style="color: #0000ff;"&gt;is</span><span style="color: #000000;"&gt; DescriptionAttribute);

        order </span>= <span style="color: #800080;"&gt;0</span><span style="color: #000000;"&gt;;
        description </span>=<span style="color: #000000;"&gt; fieldInfo.Name;

        </span><span style="color: #0000ff;"&gt;if</span> (attr != <span style="color: #0000ff;"&gt;null</span><span style="color: #000000;"&gt;)
        {
            order </span>=<span style="color: #000000;"&gt; attr.Order;
            description </span>=<span style="color: #000000;"&gt; attr.Name;
        }
        </span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; description;

    }

    </span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<summary></span>
    <span style="color: #808080;"&gt;///</span><span style="color: #008000;"&gt; 获取当前枚举的所有描述
    </span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;</summary></span>
    <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<param name="value"&gt;</param></span>
    <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<returns></returns></span>
    <span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;static</span> List<KeyValuePair<<span style="color: #0000ff;"&gt;int</span>,<span style="color: #0000ff;"&gt;string</span>>> GetAll<T><span style="color: #000000;"&gt;()
    {
        </span><span style="color: #0000ff;"&gt;return</span> GetAll(<span style="color: #0000ff;"&gt;typeof</span><span style="color: #000000;"&gt;(T));
    }

    </span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<summary></span>
    <span style="color: #808080;"&gt;///</span><span style="color: #008000;"&gt; 获取所有的枚举描述和值
    </span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;</summary></span>
    <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<param name="type"&gt;</param></span>
    <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<returns></returns></span>
    <span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;static</span> List<KeyValuePair<<span style="color: #0000ff;"&gt;int</span>,<span style="color: #0000ff;"&gt;string</span>>><span style="color: #000000;"&gt; GetAll(Type type)
    {

        List</span><EnumToolsModel> list = <span style="color: #0000ff;"&gt;new</span> List<EnumToolsModel><span style="color: #000000;"&gt;();

        </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 循环枚举获取所有的Fields</span>
        <span style="color: #0000ff;"&gt;foreach</span> (<span style="color: #0000ff;"&gt;var</span> field <span style="color: #0000ff;"&gt;in</span><span style="color: #000000;"&gt; type.GetFields())
        {
            </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 如果是枚举类型</span>
            <span style="color: #0000ff;"&gt;if</span><span style="color: #000000;"&gt; (field.FieldType.IsEnum)
            {
                </span><span style="color: #0000ff;"&gt;object</span> tmp = field.GetValue(<span style="color: #0000ff;"&gt;null</span><span style="color: #000000;"&gt;);
                Enum enumValue </span>=<span style="color: #000000;"&gt; (Enum)tmp;
                </span><span style="color: #0000ff;"&gt;int</span> intValue =<span style="color: #000000;"&gt; Convert.ToInt32(enumValue);
                </span><span style="color: #0000ff;"&gt;int</span><span style="color: #000000;"&gt; order;
                </span><span style="color: #0000ff;"&gt;string</span> showName = enumValue.GetDescription(<span style="color: #0000ff;"&gt;out</span> order); <span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 获取描述和排序</span>
                list.Add(<span style="color: #0000ff;"&gt;new</span> EnumToolsModel { Key = intValue,Order =<span style="color: #000000;"&gt; order });
            }
        }

        </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 排序并转成KeyValue返回</span>
        <span style="color: #0000ff;"&gt;return</span> list.OrderBy(i => i.Order).Select(i => <span style="color: #0000ff;"&gt;new</span> KeyValuePair<<span style="color: #0000ff;"&gt;int</span>,i.Name)).ToList();

    }


    </span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<summary></span>
    <span style="color: #808080;"&gt;///</span><span style="color: #008000;"&gt; 枚举下拉
    </span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;</summary></span>
    <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<typeparam name="T"&gt;</span><span style="color: #008000;"&gt;枚举类型</span><span style="color: #808080;"&gt;</typeparam></span>
    <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<param name="html"&gt;</param></span>
    <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<param name="htmlAttributes"&gt;</param></span>
    <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<returns></returns></span>
    <span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;static</span> MvcHtmlString EnumToolsSelect<T>(<span style="color: #0000ff;"&gt;this</span> HtmlHelper html,<span style="color: #0000ff;"&gt;object</span> htmlAttributes = <span style="color: #0000ff;"&gt;null</span><span style="color: #000000;"&gt;)
    {
        </span><span style="color: #0000ff;"&gt;return</span> html.EnumToolsSelect(<span style="color: #0000ff;"&gt;typeof</span>(T),htmlAttributes);
    }


    </span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<summary></span>
    <span style="color: #808080;"&gt;///</span><span style="color: #008000;"&gt; 枚举下拉
    </span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;</summary></span>
    <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<typeparam name="T"&gt;</span><span style="color: #008000;"&gt;枚举类型</span><span style="color: #808080;"&gt;</typeparam></span>
    <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<param name="html"&gt;</param></span>
    <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<param name="selectedValue"&gt;</span><span style="color: #008000;"&gt;选择项</span><span style="color: #808080;"&gt;</param></span>
    <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<param name="htmlAttributes"&gt;</param></span>
    <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<returns></returns></span>
    <span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;static</span> MvcHtmlString EnumToolsSelect<T>(<span style="color: #0000ff;"&gt;this</span> HtmlHelper html,<span style="color: #0000ff;"&gt;object</span> htmlAttributes = <span style="color: #0000ff;"&gt;null</span><span style="color: #000000;"&gt;)
    {
        </span><span style="color: #0000ff;"&gt;return</span> html.EnumToolsSelect(<span style="color: #0000ff;"&gt;typeof</span><span style="color: #000000;"&gt;(T),htmlAttributes);
    }

    </span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<summary></span>
    <span style="color: #808080;"&gt;///</span><span style="color: #008000;"&gt; 枚举下拉
    </span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;</summary></span>
    <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<typeparam name="T"&gt;</span><span style="color: #008000;"&gt;枚举类型</span><span style="color: #808080;"&gt;</typeparam></span>
    <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<param name="html"&gt;</param></span>
    <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<param name="selectedValue"&gt;</span><span style="color: #008000;"&gt;选择项</span><span style="color: #808080;"&gt;</param></span>
    <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<param name="htmlAttributes"&gt;</param></span>
    <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<returns></returns></span>
    <span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;static</span> MvcHtmlString EnumToolsSelect<T>(<span style="color: #0000ff;"&gt;this</span> HtmlHelper html,htmlAttributes);
    }



    </span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<summary></span>
    <span style="color: #808080;"&gt;///</span><span style="color: #008000;"&gt; 枚举下拉
    </span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;</summary></span>
    <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<param name="html"&gt;</param></span>
    <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<param name="enumType"&gt;</span><span style="color: #008000;"&gt;枚举类型</span><span style="color: #808080;"&gt;</param></span>
    <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<param name="selectedValue"&gt;</span><span style="color: #008000;"&gt;选择项</span><span style="color: #808080;"&gt;</param></span>
    <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<param name="htmlAttributes"&gt;</param></span>
    <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<returns></returns></span>
    <span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;static</span> MvcHtmlString EnumToolsSelect(<span style="color: #0000ff;"&gt;this</span> HtmlHelper html,<span style="color: #0000ff;"&gt;object</span> htmlAttributes = <span style="color: #0000ff;"&gt;null</span><span style="color: #000000;"&gt;)
    {
        </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 创建标签</span>
        TagBuilder tag = <span style="color: #0000ff;"&gt;new</span> TagBuilder(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;select</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;);

        </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 添加自定义标签</span>
        <span style="color: #0000ff;"&gt;if</span> (htmlAttributes != <span style="color: #0000ff;"&gt;null</span><span style="color: #000000;"&gt;)
        {
            RouteValueDictionary htmlAttr </span>= htmlAttributes <span style="color: #0000ff;"&gt;as</span> RouteValueDictionary ??<span style="color: #000000;"&gt; HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
            tag.MergeAttributes(htmlAttr);
        }
        </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 创建option集合</span>
        StringBuilder options = <span style="color: #0000ff;"&gt;new</span><span style="color: #000000;"&gt; StringBuilder();
        </span><span style="color: #0000ff;"&gt;foreach</span> (<span style="color: #0000ff;"&gt;var</span> item <span style="color: #0000ff;"&gt;in</span><span style="color: #000000;"&gt; GetAll(enumType))
        {
            </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 创建option</span>
            TagBuilder option = <span style="color: #0000ff;"&gt;new</span> TagBuilder(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;option</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;);

            </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 添加值</span>
            option.MergeAttribute(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;value</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;,item.Key.ToString());

            </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 设置选择项</span>
            <span style="color: #0000ff;"&gt;if</span> (item.Key ==<span style="color: #000000;"&gt; selectedValue)
            {
                option.MergeAttribute(</span><span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;selected</span><span style="color: #800000;"&gt;"</span>,<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;selected</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;);
            }

            </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 设置option</span>

<span style="color: #000000;"> option.SetInnerText(item.Value);
options.Append(option.ToString());
}
tag.InnerHtml =<span style="color: #000000;"> options.ToString();

        </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 返回MVCHtmlString</span>
        <span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; MvcHtmlString.Create(tag.ToString());

    }
    </span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<summary></span>
    <span style="color: #808080;"&gt;///</span><span style="color: #008000;"&gt; 下拉枚举
    </span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;</summary></span>
    <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<typeparam name="TModel"&gt;</span><span style="color: #008000;"&gt;Model</span><span style="color: #808080;"&gt;</typeparam></span>
    <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<typeparam name="TProperty"&gt;</span><span style="color: #008000;"&gt;属性</span><span style="color: #808080;"&gt;</typeparam></span>
    <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<param name="htmlHelper"&gt;</param></span>
    <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<param name="expression"&gt;</param></span>
    <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<param name="htmlAttributes"&gt;</param></span>
    <span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<returns></returns></span>
    <span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;static</span> MvcHtmlString EnumToolsSelectFor<TModel,<span style="color: #0000ff;"&gt;object</span> htmlAttributes = <span style="color: #0000ff;"&gt;null</span><span style="color: #000000;"&gt;)
    {
        </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 获取元数据meta</span>
        ModelMetadata modelMetadata =<span style="color: #000000;"&gt; ModelMetadata.FromLambdaExpression(expression,htmlHelper.ViewData);

        Type enumType </span>=<span style="color: #000000;"&gt; modelMetadata.ModelType;

        </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 设置id name的属性值</span>
        <span style="color: #0000ff;"&gt;var</span> rvd = <span style="color: #0000ff;"&gt;new</span><span style="color: #000000;"&gt; RouteValueDictionary
{
    { </span><span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;id</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;,modelMetadata.PropertyName }
};

        </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 添加自定义属性</span>
        <span style="color: #0000ff;"&gt;if</span> (htmlAttributes != <span style="color: #0000ff;"&gt;null</span><span style="color: #000000;"&gt;)
        {
            RouteValueDictionary htmlAttr </span>= htmlAttributes <span style="color: #0000ff;"&gt;as</span> RouteValueDictionary ??<span style="color: #000000;"&gt; HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
            </span><span style="color: #0000ff;"&gt;foreach</span> (<span style="color: #0000ff;"&gt;var</span> item <span style="color: #0000ff;"&gt;in</span><span style="color: #000000;"&gt; htmlAttr)
            {
                rvd.Add(item.Key,item.Value);
            }
        }

        </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 获取验证信息</span>
        IDictionary<<span style="color: #0000ff;"&gt;string</span>,modelMetadata);

        </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 添加至自定义属性</span>
        <span style="color: #0000ff;"&gt;if</span> (validationAttributes != <span style="color: #0000ff;"&gt;null</span><span style="color: #000000;"&gt;)
        {
            </span><span style="color: #0000ff;"&gt;foreach</span> (<span style="color: #0000ff;"&gt;var</span> item <span style="color: #0000ff;"&gt;in</span><span style="color: #000000;"&gt; validationAttributes)
            {
                rvd.Add(item.Key,item.Value);
            }
        }
        </span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; htmlHelper.EnumToolsSelect(enumType,rvd);
    }
}

</span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<summary></span>
<span style="color: #808080;"&gt;///</span><span style="color: #008000;"&gt; 枚举特性
</span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;</summary></span>
[AttributeUsage(AttributeTargets.Field,<span style="color: #0000ff;"&gt;int</span><span style="color: #000000;"&gt; order)
    {
        Name </span>=<span style="color: #000000;"&gt; name;
        Order </span>=<span style="color: #000000;"&gt; order;
    }

}


</span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;<summary></span>
<span style="color: #808080;"&gt;///</span><span style="color: #008000;"&gt; 枚举Model
</span><span style="color: #808080;"&gt;///</span> <span style="color: #808080;"&gt;</summary></span> 
<span style="color: #0000ff;"&gt;partial</span> <span style="color: #0000ff;"&gt;class</span><span style="color: #000000;"&gt; EnumToolsModel
{
    </span><span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;int</span> Order { <span style="color: #0000ff;"&gt;get</span>; <span style="color: #0000ff;"&gt;set</span><span style="color: #000000;"&gt;; }
    </span><span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;string</span> Name { <span style="color: #0000ff;"&gt;get</span>; <span style="color: #0000ff;"&gt;set</span><span style="color: #000000;"&gt;; }
    </span><span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;int</span> Key { <span style="color: #0000ff;"&gt;get</span>; <span style="color: #0000ff;"&gt;set</span><span style="color: #000000;"&gt;; }
}

}

最后望对各位有所帮助,本文原创,欢迎拍砖和推荐。 ??

Github:

系列课程

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读