Asp.Net MVC 2 LabelFor自定义文本

时间:2010-01-15 23:46:40

标签: asp.net-mvc

有没有办法使用LabelFor助手并自定义标签文本而无需在我的模型中使用DisplayNameAttribute?

4 个答案:

答案 0 :(得分:20)

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

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);
    }
}

我将它们用于“强类型”资源:

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

希望有帮助...

答案 1 :(得分:14)

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

答案 2 :(得分:1)

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

public static class MVCHelpers
{
    public static string CustomLabelFor(this HtmlHelper helper, string ...)

    {
        return "<label ... </label>"
    }
}

答案 3 :(得分:1)

我发现这非常有用。我认为这是MVC 2中缺少的东西。或者至少我没有找到一种方法来构建它。

最简单的案例详细说明了此功能的必要性。我有两个对象联系人和地址。联系人可以有多个Addesses

地址

  • ID
  • 国家

联系

  • ID
  • 总线地址ID
  • 家庭住址ID

现在,对于编辑或显示联系人的表单,能够更改地址“街道”属性上的DisplayNameAttribute并不是很有帮助,因为我真的希望一个字段为“商业街”而另一个字段为“家庭街” ”。

相关问题