Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在数据批注的ErrorMessage属性中使用html_C#_Asp.net Mvc 3_Data Annotations - Fatal编程技术网

C# 在数据批注的ErrorMessage属性中使用html

C# 在数据批注的ErrorMessage属性中使用html,c#,asp.net-mvc-3,data-annotations,C#,Asp.net Mvc 3,Data Annotations,任何人都知道是否可以执行以下操作: 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:suppo

任何人都知道是否可以执行以下操作:

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
}
公共类用户
{
公共Guid用户标识{get;set;}
[必需(ErrorMessage=“需要学校选择。”)]
[范围(1,int.MaxValue,ErrorMessage=“未找到学校。请通过以下地址与支持部门联系”)]
公共int SchoolID{get;set;}
//……其他财产
}
并且没有
@Html.ValidationMessageFor(model=>model.SchoolID)
编码Html?也许我需要为这个做一个自定义的助手扩展?有没有更简单的方法?任何指导都将不胜感激。如果要使用helper扩展,我如何访问helper扩展方法中的验证规则以确保html没有被编码


谢谢。

根据您需要使用的频率,您可以在验证消息上使用
HttpUtility.HtmlDecode
方法

@{HttpUtility.HtmlDecode(Html.ValidationMessageFor(x=>x.SchoolId).ToString)}
更具可重用性但涉及更多前期时间的方法是创建自己的html助手(可能称为HtmlValidationMessageFor()),该助手返回非html编码的字符串

话虽如此,ValidationMessageFor函数的返回类型是一个

表示不应再次编码的HTML编码字符串


但是我找不到一个确切的理由来解释为什么不应该再次对它进行编码,除了它可能是一个双重编码的字符串。

我做了更多的搜索,并在下面的SO帖子中找到了如何实现我的要求。我在这里补充了这个答案,以防将来有人遇到这个问题。我也会把这个标记为答案,因为它最接近我想要的最佳答案,但我会让版主决定是否需要将其作为副本关闭


我只需要解决一条错误消息。所以简单的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>");
            }
       });
    });

$(文档).ready(函数(){
$('.validation summary errors ul li')。每个(函数(){
如果($(this.html()==“未找到学校”){
$(this.html(“未找到学校。请通过联系支持”);
}
});
});

干杯

实际上,我在使用MvcHtmlString.Create()时使用了HTML,它输出到视图而不进行编码,到目前为止效果很好。我假设这就是问题所在,因为标准的helper方法就是通过生成html并通过MvcHtmlString输出它来工作的。实际上,现在的问题是如何从helper扩展方法中获取范围属性ErrorMessage text,并将其重新创建为MvcHtmlString?