Asp.Net MVC 2 LabelFor自定义文本 [英] Asp.Net MVC 2 LabelFor Custom Text

查看:118
本文介绍了Asp.Net MVC 2 LabelFor自定义文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有使用LabelFor助手和自定义标签文本,而无需使用DisplayNameAttribute在我的模型的方法吗?

Is there a way to use the LabelFor helper and customize the label text without having to use the DisplayNameAttribute in my model?

推荐答案

我创建这个网站的助手为我的项目:

I have created this html helpers for my project:

public static class MyLabelExtensions
{
    public static MvcHtmlString Label(this HtmlHelper htmlHelper, string forName, string labelText)
    {
        return Label(htmlHelper, forName, labelText, (object) null);
    }

    public static MvcHtmlString Label(this HtmlHelper htmlHelper, string forName, string labelText,
                                      object htmlAttributes)
    {
        return Label(htmlHelper, forName, labelText, new RouteValueDictionary(htmlAttributes));
    }
    public static MvcHtmlString Label(this HtmlHelper htmlHelper, string forName, string labelText,
                                      IDictionary<string, object> htmlAttributes)
    {
        var tagBuilder = new TagBuilder("label");
        tagBuilder.MergeAttributes(htmlAttributes);
        tagBuilder.MergeAttribute("for", forName.Replace(".", tagBuilder.IdAttributeDotReplacement), true);
        tagBuilder.SetInnerText(labelText);
        return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.Normal));
    }

    public static MvcHtmlString LabelFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
                                                            Expression<Func<TModel, TProperty>> expression,
                                                            string labelText)
    {
        return LabelFor(htmlHelper, expression, labelText, (object) null);
    }
    public static MvcHtmlString LabelFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
                                                            Expression<Func<TModel, TProperty>> expression,
                                                            string labelText, object htmlAttributes)
    {
        return LabelFor(htmlHelper, expression, labelText, new RouteValueDictionary(htmlAttributes));
    }
    public static MvcHtmlString LabelFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
                                                            Expression<Func<TModel, TProperty>> expression,
                                                            string labelText,
                                                            IDictionary<string, object> htmlAttributes)
    {
        string inputName = ExpressionHelper.GetExpressionText(expression);
        return htmlHelper.Label(inputName, labelText, htmlAttributes);
    }
}

我使用它们以强类型的资源:

I use them with "strongly typed" resources:

<%= Html.LabelFor(m=>m.NickName, UserStrings.NickName) %>

希望帮助...

Hope that helps...

这篇关于Asp.Net MVC 2 LabelFor自定义文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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