Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 3 Display HTML inside a ValidationSummary

I am trying to display a strong tag inside a validation summary but it encodes it and does not display properly.

@Html.ValidationSummary(false, "<strong>ERROR:<strong>The form is not valid!")

How can I get this to work?

like image 370
Chris Kooken Avatar asked Feb 15 '11 15:02

Chris Kooken


2 Answers

The easiest way:

@if (!ViewData.ModelState.IsValid)
{
<div>@Html.Raw(HttpUtility.HtmlDecode(Html.ValidationSummary(false, "<strong>ERROR:<strong>The form is not valid!").ToHtmlString()))</div>
}
like image 146
r a Avatar answered Sep 27 '22 23:09

r a


I've find this :

    public static MvcHtmlString ToMvcHtmlString(this MvcHtmlString htmlString)
    {
        if (htmlString != null)
        {
            return new MvcHtmlString(HttpUtility.HtmlDecode(htmlString.ToString()));
        }
        return null;
    }

and then :

@Html.ValidationSummary(false, "<strong>ERROR:<strong>The form is not valid!").ToMvcHtmlString()
like image 40
jvlamy Avatar answered Sep 27 '22 22:09

jvlamy