views:

169

answers:

1

I refer to this question.

The answer offered works really well but it doesn't seem to take the data annotation attributes for the DisplayNameAttribute into account when using the new labelfor helper.

This is the code I am using:

public static MvcHtmlString LabelFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, string prefix, Expression<Func<TModel, TProperty>> expression, IDictionary<string, object> htmlAttributes)
    {
        return htmlHelper.Label(String.Format("{0}.{1}", prefix, expression));
    }

Is there any way around this or am I just going to have to use a normal Label helper?

A: 

I think that only the DisplayFor and EditorFor (templating) extensions take the DataAnnotation attributes into account. If neither of those will work for you out of the box, you could create your own templates. Rather than detailing how here, I'll simply refer you to Brad Wilson's excellent blog posts on templating in MVC2. The alternative, as you've guessed, is simply to use the non-strongly-typed helpers.

tvanfosson