试图修改一个生成器样式文本DataAnnotation属性[显示(名称="文本和QUOT;) [英] Trying to modify a builder to style text in DataAnnotation Attribute [Display(Name="Text")]

查看:135
本文介绍了试图修改一个生成器样式文本DataAnnotation属性[显示(名称="文本和QUOT;)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问一个问题,并得到了我的具体问题有很大的答案,但有&LT碰到一个问题;选择>使用列出&LT ;标签> 并试图风格的DataAnnotation属性 [显示(NAME =文本)] 的东西,如 [显示(NAME =文字<跨度类= \\MyClass的\\>特殊样式的文字< / SPAN方式>)。]

I asked a question and got a great answer for my specific problem, but have run into a problem with <select> lists using <label> and trying to style the DataAnnotation Attribute [Display(Name="Text")] with something like [Display(Name="Text <span class=\"myclass\">Special styled text</span>.")].

下面是<一个href=\"http://stackoverflow.com/questions/9809223/any-way-to-style-text-contained-in-dataannotation-attribute-displayname-tex/9818056#9818056\">Question/Answer链接。

答案解释关于HTML编码和presented我为 Html.LabelFor 助手的解决方案。它完美地工作对我的眼前的情形。不过,后来我发现,自从我使用code创建&LT;选择&GT; 名单,新的帮手不是被它挑出来。任何帮助是极大的AP preciated。这里是&LT;选择&GT; 建设者code我使用的:

The answer explained about Html Encoding and presented me with a solution for the Html.LabelFor helper. It worked perfectly for my immediate situation. However, I later discovered that since I am using code to create <select> lists, the new helper is not being picked up by it. Any help is greatly appreciated. Here is the <select> builder code I am using:

public MvcHtmlString BuildInput(string fieldName, 
    SelectListItem item, string inputType)
{
    var id = ViewData.TemplateInfo.GetFullHtmlFieldId(item.Value);
    var wrapper = new TagBuilder("div");
    wrapper.AddCssClass("selector-item");

    var input = new TagBuilder("input");
    input.MergeAttribute("type", inputType);
    input.MergeAttribute("name", fieldName);
    input.MergeAttribute("value", item.Value);
    input.MergeAttribute("id", id);

    input.MergeAttributes(Html.GetUnobtrusiveValidationAttributes
        (fieldName, ViewData.ModelMetadata));

    if(item.Selected)
        input.MergeAttribute("checked", "checked");

    wrapper.InnerHtml += input.ToString(TagRenderMode.SelfClosing);


    var label = new TagBuilder("label"); // tried merging code around 
                                         // here but got it all wrong
    label.MergeAttribute("for", id);
    label.SetInnerText(item.Text);
    wrapper.InnerHtml += label;

    return new MvcHtmlString(wrapper.ToString());
}

我在code我试图合并帮手code发挥各地指出,但不​​能得到它的权利。

As I indicated in the code I played around with trying to merge the helper code but could not get it right.

再次任何帮助是AP preciated。

Again, any help is appreciated.

推荐答案

只需更换:

label.SetInnerText(item.Text);

label.InnerHtml = item.Text;

这篇关于试图修改一个生成器样式文本DataAnnotation属性[显示(名称=&QUOT;文本和QUOT;)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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