单选按钮枚举助手与资源文件模型 [英] Radio Button Enum Helper for model with resource file

查看:77
本文介绍了单选按钮枚举助手与资源文件模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

需要绑定具有性别作为枚举属性强类型的模型。此外,我喜欢展示从资源文件中的显示文本。

我的模型

 公开枚举GenderViewModel
{
  [显示(NAME =男的ResourceType = typeof运算(Resources.Global),令= 0)]
  男,
  [显示(NAME =女的ResourceType = typeof运算(Resources.Global),令= 1)]
  女}

起初,我尝试如下的<一个href=\"http://romikoderbynew.com/2012/02/23/asp-net-mvc-rendering-enum-dropdownlists-radio-buttons-and-listboxes/\" rel=\"nofollow\">http://romikoderbynew.com/2012/02/23/asp-net-mvc-rendering-enum-dropdownlists-radio-buttons-and-listboxes/

但它有点复杂,我无法但是我想纠正我的HTML。

然后我一看,从计算器简单,易于实现,通枚举为HTML .radiobuttonfor MVC3

和使用像一个CSHTML下面的HtmlHelper

  @ Html.RadioButtonForEnum(M = GT; m.Gender)

HTML制作

 &LT;标签=_ Gender_Male&GT;
 &LT;输入类型=无线电VALUE =男NAME =性别ID =_ Gender_Male
 数据-VAL-所需=性别需要数据-VAL =真检查=选中&GT;
 &LT;跨度类=radiotext&GT;男&LT; / SPAN&GT;
&LT; /标签&gt;&LT;标签=_ Gender_Female&GT;
&LT;输入类型=无线电VALUE =女NAME =性别ID =_ Gender_Female&GT;
&LT;跨度类=radiotext&GT;女&LT; / SPAN&GT;&LT; /标签&gt;


  

这真的简单,很适合我。但我没有得到的值
  从资源文件。我的应用程序是多语言和我使用的是全球
  资源文件为不同的语言支持。


问题:

显示应 女性显示应 Kvinna 应该从资源文件,因为我目前的文化 SV-SE

可以在任何一个可以帮助/提供其中有超过HTML很好的控制了一个简单的解决方案?


解决方案

所有你需要做的就是适应 我原来的帮手 ,以便它考虑到了 DisplayAttribute

 公共静态类HtmlExtensions
{
    公共静态MvcHtmlString RadioButtonForEnum&LT;的TModel,TProperty&GT;(
        这与的HtmlHelper LT;的TModel&GT;的HtmlHelper,
        防爆pression&LT;&Func键LT;的TModel,TProperty&GT;&GT;前pression
    )
    {
        VAR元= ModelMetadata.FromLambdaEx pression(如pression,htmlHelper.ViewData);
        如果(!metaData.ModelType.IsEnum)
        {
            抛出新的ArgumentException(这个助手用来与枚举类型使用);
        }        变种名称= Enum.GetNames(metaData.ModelType);
        VAR SB =新的StringBuilder();        VAR栏= metaData.ModelType.GetFields(
            BindingFlags.Static | BindingFlags.GetField | BindingFlags.Public
        );        的foreach(在名称变量名称)
        {
            VAR ID =的String.Format(
                {0} _ {1} _ {2},
                htmlHelper.ViewData.TemplateInfo.HtmlField preFIX,
                metaData.PropertyName,
                名称
            );
            VAR无线电= htmlHelper.RadioButtonFor(如pression,名称,新的{ID = ID})ToHtmlString()。
            VAR字段= fields.Single(F =&GT; f.Name ==名);
            VAR标签=名;
            VAR显示=场
                .GetCustomAttributes(typeof运算(DisplayAttribute),FALSE)
                .OfType&LT; D​​isplayAttribute&GT;()
                .FirstOrDefault();
            如果(显示!= NULL)
            {
                标签= display.GetName();
            }            sb.AppendFormat(
                &LT;标签= \\{0} \\&GT; {1}&LT; /标签&gt; {2},
                ID,
                HttpUtility.HtmlEn code(标签),
                无线电
            );
        }
        返回MvcHtmlString.Create(sb.ToString());
    }
}

现在,如果你装饰了一些与DisplayAttribute枚举值的,值将来自资源文件。

Problem:

Need to bind a strongly typed model which has a Gender as enum property. Also i like to show a Display text from a Resource file.

My Model is

public enum GenderViewModel
{
  [Display(Name = "Male", ResourceType = typeof(Resources.Global), Order = 0)]
  Male,
  [Display(Name = "Female", ResourceType = typeof(Resources.Global), Order = 1)]
  Female

}

Initially, I tried following http://romikoderbynew.com/2012/02/23/asp-net-mvc-rendering-enum-dropdownlists-radio-buttons-and-listboxes/

But it was bit complex and i was unable to correct my HTML however i want.

Then i had a look of simple and easy implementation from stackoverflow, pass enum to html.radiobuttonfor MVC3

and used a HtmlHelper in cshtml like below

@Html.RadioButtonForEnum(m => m.Gender)

HTML Produced

<label for="_Gender_Male">
 <input type="radio" value="Male" name="Gender" id="_Gender_Male" 
 data-val-required="Gender is required" data-val="true" checked="checked">
 <span class="radiotext">Male</span>
</label>

<label for="_Gender_Female">
<input type="radio" value="Female" name="Gender" id="_Gender_Female">
<span class="radiotext">Female</span></label>

It really simple and works well for me. But i am not getting values from Resource files. My application is multilingual and I use a Global Resource file for different language support.

Issue:

Male displayed should be Man and Female displayed should be Kvinna should be from Resource file, as my current culture is sv-se

Could any one help/ provide a simple solution which has a good control over HTML?

解决方案

All you have to do is adapt my original helper so that it takes into account the DisplayAttribute:

public static class HtmlExtensions
{
    public static MvcHtmlString RadioButtonForEnum<TModel, TProperty>(
        this HtmlHelper<TModel> htmlHelper,
        Expression<Func<TModel, TProperty>> expression
    )
    {
        var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
        if (!metaData.ModelType.IsEnum)
        {
            throw new ArgumentException("This helper is intended to be used with enum types");
        }

        var names = Enum.GetNames(metaData.ModelType);
        var sb = new StringBuilder();

        var fields = metaData.ModelType.GetFields(
            BindingFlags.Static | BindingFlags.GetField | BindingFlags.Public
        );

        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();
            var field = fields.Single(f => f.Name == name);
            var label = name;
            var display = field
                .GetCustomAttributes(typeof(DisplayAttribute), false)
                .OfType<DisplayAttribute>()
                .FirstOrDefault();
            if (display != null)
            {
                label = display.GetName();
            }

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

Now if you have decorated some of the enum values with the DisplayAttribute, the values will come from the resource file.

这篇关于单选按钮枚举助手与资源文件模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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