baio
baio

Reputation: 1172

Template for ValidationMessage

MVC now contains a lot of stuff that you can ovrride using templates, you could override UI representation of any model's field based on any rule and its great. But why I couldn't do the same for the validation message, I suppose it would give developers more flexibility and convience. I know what I could use overriding of Extensions methods just like this:

public static MvcHtmlString ValidationMessage(this HtmlHelper Html, string Model)
   {
     return System.Web.Mvc.Html.ValidationExtensions.ValidationMessage(Html, Model);
   }

But as long with this one I should also override ValidationMessageFor, also it is divert from the general implementation of html code templating for MVC.

Question: that is the best practise to solve problem of this kind?

Thanks!

Upvotes: 2

Views: 960

Answers (1)

Spikeh
Spikeh

Reputation: 3695

When I've needed to customise my ValidationMessageFor elements, I've used css to target the rendered tag.

The only templates I'm aware of are EditorTemplates and DisplayTemplates - which require "EditorFor" and "DisplayFor" respectively. ValidationMessageFor is hard coded.

I did find some posts here and here, which should help you write your own method.

Upvotes: 1

Related Questions