tags:

views:

1224

answers:

3

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

+1  A: 

Why not just create your own Html Helper?

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

    {
        return "<label ... </label>"
    }
}
Martin
That was going to be my last resort, was making sure I wasn't missing something. It seems strange that the only way i've found to customize the text is using the DisplayNameAttribute.
B Z
+10  A: 

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...

rrejc
I just came across this code and implemented it in my project. However, I did get some weird "Inconsistent accessibility" message for HtmlHelper when building. I solved this by replacing every instance of "this HtmlHelper" to "this System.Web.Mvc.HtmlHelper". Hope that helps anyone else who finds this.
Colin O'Dell
+1  A: 

I have found this extemely useful. I think this is something missing from MVC 2. Or at least I have not found a way to do it built in.

The simplest case details the need for this feature. I have two objects Contacts and Addresses. A Contact can have multiple Addesses

Address

  • ID
  • Street
  • City
  • State

Contact

  • ID
  • Bus Address ID
  • Home Address ID

Now for a form which edits or displays a Contact, being able to change the DisplayNameAttribute on the Address 'Street' property is not very helpful since I really want one field to be 'Business Street' and another to be 'Home Street'.

Mike Gasser
I agree. I think there should be a way to overwrite.
B Z