ModelState.AddModelError encodes HTML

Posted by NTulip on Stack Overflow See other posts from Stack Overflow or by NTulip
Published on 2009-04-07T19:03:50Z Indexed on 2010/03/14 20:25 UTC
Read the original article Hit count: 418

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

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about input-validation