开发者

ASPNET MVC 2 LabelFor prefix overload

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 w开发者_StackOverflow社区hen 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜