views:

141

answers:

2

Hi,

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

A: 

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.

tvanfosson
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?
Neil
A: 

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!

Neil
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.
tvanfosson