首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Asp.Net MVC 2 LabelFor自定义文本

Asp.Net MVC 2 LabelFor自定义文本
EN

Stack Overflow用户
提问于 2010-01-16 07:46:40
回答 3查看 10.3K关注 0票数 18

有没有一种方法可以在不使用我的模型中的DisplayNameAttribute的情况下使用LabelFor助手和自定义标签文本?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-01-23 17:50:58

我已经为我的项目创建了这个html助手:

代码语言:javascript
复制
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);
    }
}

我将它们与“强类型”资源一起使用:

代码语言:javascript
复制
<%= Html.LabelFor(m=>m.NickName, UserStrings.NickName) %>

希望这能帮上忙。

票数 20
EN

Stack Overflow用户

发布于 2010-11-11 08:45:36

MVC3 RC中有一个新的LabelFor重载,允许您指定labelText。

票数 14
EN

Stack Overflow用户

发布于 2010-01-16 10:13:42

为什么不创建你自己的Html Helper呢?

代码语言:javascript
复制
public static class MVCHelpers
{
    public static string CustomLabelFor(this HtmlHelper helper, string ...)

    {
        return "<label ... </label>"
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2075493

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档