13

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?

Chris Kooken
  • 32,730
  • 15
  • 85
  • 123

5 Answers5

39

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>
}
r a
  • 441
  • 4
  • 8
6

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()
jvlamy
  • 61
  • 1
  • 2
6
@Html.Raw(System.Web.HttpUtility.HtmlDecode((Html.ValidationSummary(false) ?? (object)"").ToString()))
Tomas Kubes
  • 23,880
  • 18
  • 111
  • 148
3

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.

Community
  • 1
  • 1
Rob West
  • 5,189
  • 1
  • 27
  • 42
  • This is not the correct answer. See below correct answer by @ra. This answer only shows a good way of using ValidationSummary. But not how to show html in ValidationSummary. – Gautam Jain Sep 17 '20 at 17:40
-4

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.

Howard Taylor
  • 23
  • 1
  • 4