views:

977

answers:

3

Hi

I am noticing a weird issue when using ModelState.AddModelError to validate input on my forms. The output from Html.ValidationMessage is not the true HTML value but it's encoded value and so the CSS style is not applied to the error message.

Example:

private string errorMessage = "<span class=\"negative\">{0}</span><br class=\"hid\" />";
ModelState.AddModelError("title", String.Format(errorMessage, "Tab title is required"));

The output is shown as:

<span class="field-validation-error">&lt;span class=&quot;negative&quot;&gt;URL is Required&lt;/span&gt;&lt;br class=&quot;hid&quot; /&gt;</span>

This didn't use to be the case with their earlier beta's and I am not sure what approach to take here.

Thanks Nick

+1  A: 

Create your own extension method that mimics Html.VallidationMessage...?

I had to do something similar because the built in MVC validation stuff (ModelState, ValidationMessage etc etc) doesn't cater for pages that have more than one form on a page.

Charlino
you're right - i had updated the wrong item. They were both related to encoding of the html - i just had updated the wrong one. I've removed the update. thanks for noticing.
NTulip
A: 

If it can help you here is the one we made for our use (it's actually just MVC original ValidationSummary code changed):

http://numeus.com/code/ValidationExtensions.txt

Similar way you can make same thing for the "ValidationMessage".

Andrej Kaurin
cool - thanks for sharing.
NTulip
A: 

Could anybody share this code? http://numeus.com/code/ValidationExtensions.txt there is nothing on this link :(

bigb