使用[显示(名称=" X")与枚举。在MVC3 ASP.Net自定义的HtmlHelper [英] Using [Display(Name = "X")] with an enum. Custom HtmlHelper in MVC3 ASP.Net

查看:61
本文介绍了使用[显示(名称=" X")与枚举。在MVC3 ASP.Net自定义的HtmlHelper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时通讯使用code的片段从另一个计算器问题:

Im using a snippet of code from another stackoverflow question:

namespace MvcHtmlHelpers
{
    public static class htmlHelpers
    {
        /// <summary>
        /// Radio button for : Adapted to support enum labels from display attributes
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static MvcHtmlString RadioButtonForEnum<TModel, TProperty>(
            this HtmlHelper<TModel> htmlHelper,
            Expression<Func<TModel, TProperty>> expression
        )
        {
            var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
            var names = Enum.GetNames(metaData.ModelType);
            var sb = new StringBuilder();
            foreach (var name in names)
            {
                var id = string.Format(
                    "{0}_{1}_{2}",
                    htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix,
                    metaData.PropertyName,
                    name
                );

                var radio =  htmlHelper.RadioButtonFor(expression, name, new { id = id }).ToHtmlString();
                sb.AppendFormat(
                    "<label for=\"{0}\">{1}</label> {2}",
                    id,
                    HttpUtility.HtmlEncode(name),
                    radio
                );
            }
            return MvcHtmlString.Create(sb.ToString());
        }
    }
}

沿边我目前的枚举

public enum Options
    {
       [Display(Name ="Yes")]
        0,
       [Display(Name = "No")]
        1,
      [Display(Name = "Other")]
        2,
    }

如果我再使用 @ html.RadioButtonForEnum(...)它显示我的枚举与相应的框中选择没有问题。不过,我想他们使用的标签值 [显示(名称=&LT;文字和GT;)] ,使他们更有意义! - 即不只是0,1,2

If I then use @html.RadioButtonForEnum(...) It displays my enums with the appropriate box selected no problem. However, I want them to use the label value [Display(Name = "<text>")] so they make more sense! - i.e. not just 0,1,2.

推荐答案

您可以使用反射来获取值:

You could use reflection to fetch the value:

public static MvcHtmlString RadioButtonForEnum<TModel, TProperty>(
    this HtmlHelper<TModel> htmlHelper,
    Expression<Func<TModel, TProperty>> expression
)
{
    var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);

    var sb = new StringBuilder();
    var enumType = metaData.ModelType;
    foreach (var field in enumType.GetFields(BindingFlags.Static | BindingFlags.GetField | BindingFlags.Public))
    {
        var value = (int)field.GetValue(null);
        var name = Enum.GetName(enumType, value);
        var label = name;
        foreach (DisplayAttribute currAttr in field.GetCustomAttributes(typeof(DisplayAttribute), true))
        {
            label = currAttr.Name;
            break;
        }

        var id = string.Format(
            "{0}_{1}_{2}",
            htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix,
            metaData.PropertyName,
            name
        );
        var radio = htmlHelper.RadioButtonFor(expression, name, new { id = id }).ToHtmlString();
        sb.AppendFormat(
            "<label for=\"{0}\">{1}</label> {2}",
            id,
            HttpUtility.HtmlEncode(label),
            radio
        );
    }
    return MvcHtmlString.Create(sb.ToString());
}

这篇关于使用[显示(名称=&QUOT; X&QUOT;)与枚举。在MVC3 ASP.Net自定义的HtmlHelper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆