简体   繁体   English

在数据注释的ErrorMessage属性中使用html

[英]Using html in ErrorMessage property of data annotations

Anyone know if it is possible to do the following: 任何人都知道是否可以执行以下操作:

public class User
{
    public Guid UserID { get; set; }

    [Required(ErrorMessage="A school selection is required.")]
    [Range(1, int.MaxValue, ErrorMessage="School not found.  Please contact support at <a href='mailto:support@mysite.com'>support@mysite.com</a>")]
    public int SchoolID { get; set; }

    // ... other properties
}

And not have @Html.ValidationMessageFor(model => model.SchoolID) encode the HTML? 并没有@Html.ValidationMessageFor(model => model.SchoolID)编码HTML? Maybe I need to make a custom helper extension for this? 也许我需要为此制作一个自定义助手扩展? Is there an easier way? 有更容易的方法吗? Any guidance would be appreciated. 任何指导将不胜感激。 If a helper extension is the way to go, how could I access the validation rules in the helper extension method to make sure the html doesn't get encoded? 如果要使用辅助扩展,我怎样才能访问辅助扩展方法中的验证规则以确保html不被编码?

Thanks. 谢谢。

Depending on how often you needed to use this, you could use the HttpUtility.HtmlDecode method on the validation message. 根据您需要使用它的HttpUtility.HtmlDecode ,您可以在验证消息上使用HttpUtility.HtmlDecode方法。

@{HttpUtility.HtmlDecode(Html.ValidationMessageFor(x=>x.SchoolId).ToString)}

The more reusable, but more upfront time involved method would be to create your own html helper (maybe called HtmlValidationMessageFor()) that returns a non-html encoded string. 更可重用但更多的前期时间方法是创建自己的html帮助器(可能称为HtmlValidationMessageFor()),它返回非html编码的字符串。

With that said, the return type for the ValidationMessageFor function is an MvcHtmlString which states that it is 话虽如此,ValidationMessageFor函数的返回类型是一个MvcHtmlString ,它声明它是

Represents an HTML-encoded string that should not be encoded again. 表示不应再次编码的HTML编码字符串。

But I could not find a solid reason as to why this should not be encoded again except that it may be a double encoded string. 但我找不到一个可靠的理由,为什么不应该再次编码,除了它可能是一个双重编码的字符串。

I did some more searching and came across the SO post below that addresses how to do what I am asking. 我做了一些搜索,发现下面的SO帖子解决了如何做我要问的问题。 I've added that answer here in case anyone comes across this in the future. 我已经在这里添加了答案,以防将来有人遇到这个问题。 I'm going to mark this as the answer as well since it is closest to the best answer I was looking for, but I'll let the moderators decide if this needs to be closed as a duplicate. 我将把这个标记作为答案,因为它最接近我正在寻找的最佳答案,但我会让主持人决定是否需要将其作为副本关闭。

Render HTML tags inside of HTML.ValidationMessageFor in MVC3 在MVC3中的HTML.ValidationMessageFor内部呈现HTML标记

I just needed a workaround for one error message. 我只需要一个错误消息的解决方法。 So a simple jquery detour; 所以一个简单的jquery绕道而行;

 <script>
    $(document).ready(function() {
       $('.validation-summary-errors ul li').each(function(){
            if($(this).html() == "School not found"){
                $(this).html("School not found.  Please contact support at <a href='mailto:support@mysite.com'>support@mysite.com</a>");
            }
       });
    });

Cheers! 干杯!

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

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