4

Just starting with ASP.Net MVC and have hit a bit of a snag regarding validation messages. I've a custom validation attribute assigned to my class validate several properties on my model.

When this validation fails, we'd like the error message to contain XHTML mark-up, including a link to help page, (this was done in the original WebForms project as a ASP:Panel).

At the moment the XHTML tags such as "< a >", in the ErrorMessage are being rendered to the screen. Is there any way to get the ValidationSummary to render the XHTML markup correctly? Or is there a better way to handle this kind of validation?

Thanks

5 Answers 5

7

Here's a short-term fix that uses HtmlDecode() to reverse the encoding. Works for me.

(Couldn't be bothered rebuilding the whole Validation object model.)

public static class ValidationExtensions
{
  public static MvcHtmlString ValidationMessageHtmlFor<TModel, TProperty>(
    this HtmlHelper<TModel> htmlHelper,
    Expression<Func<TModel, TProperty>> expression)
  {
    return new MvcHtmlString(
      HttpUtility.HtmlDecode(
        htmlHelper.ValidationMessageFor<TModel, TProperty>(
        expression, 
        null, 
      ((IDictionary<string, object>)new RouteValueDictionary()))
        .ToHtmlString()));
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, works great. For anyone else using this answer: don't forget to include using System.Web.Mvc.Html; at the top of your file or it won't find the htmlHelper.
2

I'm pretty sure that the default validation message helpers HTML encode any message that you might have in your attribute. My suggestion would be to use the source code available on CodePlex as a starting point to write your own HtmlHelper extension that doesn't do HTML encoding on the error string. You want to look in the System.Web.Mvc.Html namespace for the ValidationExtensions.cs file.

1 Comment

Ultimately it uses the following method I think: public void SetInnerText(string innerText) { this.InnerHtml = HttpUtility.HtmlEncode(innerText); } But as I'm using ValidationSummary to show messsages, I guess this means I can't use it for messages that contain XHTML. I guess I could roll my own ValidationSummary, using the source in the System.Web.Mvc.Html namespace but remove encoding call?
2

I ended up just taking the ValidationSummary output, and just HtmlDecode it.

Works great with ModelState errors in html, and I don't have to make my own ValidationSummary.

public static MvcHtmlString ValidationSummaryNoEncode(this HtmlHelper htmlHelper)
{
    string validationSummaryOutput = htmlHelper.ValidationSummary().ToHtmlString();

    string decodedValidationSummaryOutput = HttpUtility.HtmlDecode(validationSummaryOutput);

    return MvcHtmlString.Create(decodedValidationSummaryOutput);
}

Comments

0

You can also use the Html.Raw and HttpUtility.HtmlDecode helper methods in a view to render validation messages that contain HTML markup. Here's a simple example:

Model:

[Required(ErrorMessage = "This message contains <b>HTML markup</b> code.")]
public string MyProperty{ get; set; }

View:

@Html.Raw(HttpUtility.HtmlDecode(Html.ValidationMessageFor(x => x.MyProperty).ToHtmlString()))

Comments

-1

OK, thanks to tvanfosson for the suggestion about looking at the source code.

I essentially rolled my own "Html.ValidationSummaryXHTML" helper that didn't HtmlEncode any error message in ModelState, just deferred to "InnerHtml".

Works a treat!

1 Comment

Usu. you'd want to comment on your question or edit it instead of adding an answer. Also, the way the system works is you upvote answers that are helpful (once you get 15 rep) and accept (use the checkmark) the answer that best solves your problem. All of this is in the FAQ (see link at top of page). You can accept your own answer if it is the best, but you have to wait a couple of days. Welcome to the site.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.