简体   繁体   English

MVC 3在ValidationSummary中显示HTML

[英]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? 我怎样才能让它发挥作用?

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 . 您可以按照此问题的接受答案中的建议扩展ValidationSummary助手。

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. 在其中一个项目中,我将其放置为值: <img src="images/exclamation.png" > <strong>Pharmacy Name is required</strong>并且它有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM