TiagoDias
TiagoDias

Reputation: 1865

MVC ValidationMessageFor

I want to show a image instead of text when validation fails but ValidationMessageFor encodes my 'validationMessage'. I'm trying to specify a validationMessage using a plain img tag declaration How can i do that?

Thanks

Upvotes: 0

Views: 1089

Answers (2)

Roberto Conte Rosito
Roberto Conte Rosito

Reputation: 2098

you can declare a const string containing image formatted in html like:

const string ERROR_IMAGE = "<img src=\"...\"  alt=\"\" />";

in the View Action you can do something like this:

if(Something is Ine error)
   ModelState.AddModelError(Something, ERROR_IMAGE);

Upvotes: 0

Lee Smith
Lee Smith

Reputation: 6737

You will need to create your own HTMLHelper.

Something like:

 public static MvcHtmlString   ValidationImage(this HtmlHelper htmlHelper)
    {

        if (!htmlHelper.ViewData.ModelState.IsValid)
        {
                  //todo: strip out the information you need from the model and  return <img/> tag(s).    
        }

        return null;

    }

Upvotes: 1

Related Questions