Chris Kooken
Chris Kooken

Reputation: 33870

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?

Upvotes: 13

Views: 13577

Answers (5)

Howard Taylor
Howard Taylor

Reputation: 23

I have a site that uses Resource files for language. In one of the items I placed this for the Value: <img src="images/exclamation.png" > <strong>Pharmacy Name is required</strong> and it works.

Upvotes: -4

Tomas Kubes
Tomas Kubes

Reputation: 25098

@Html.Raw(System.Web.HttpUtility.HtmlDecode((Html.ValidationSummary(false) ?? (object)"").ToString()))

Upvotes: 7

r a
r a

Reputation: 441

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>
}

Upvotes: 39

jvlamy
jvlamy

Reputation: 61

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()

Upvotes: 6

Rob West
Rob West

Reputation: 5241

You could extend the ValidationSummary helper as has been suggested in the accepted answer on this question.

Edit: I presume the encoding of any text entered is a security feature and therefore a good thing.

Upvotes: 3

Related Questions