开发者

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开发者_JAVA百科 get this to work?


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


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


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜