ASP.NET MVC – 将XHTML添加到验证错误消息中

刚刚开始使用ASP.Net MVC并且在验证消息方面遇到了一些障碍.
我有一个分配给我的类的自定义验证属性验证我的模型上的几个属性.

当此验证失败时,我们希望错误消息包含XHTML标记,包括指向帮助页面链接(这是在原始WebForms项目中作为ASP:Panel完成的).

目前,ErrorMessage中的XHTML标记(例如“< a>”)正在呈现给屏幕.有没有办法让ValidationSummary正确呈现XHTML标记?或者有更好的方法来处理这种验证吗?

谢谢

解决方法

这是一个使用 HtmlDecode()来反转编码的短期修复.适合我.

(无法重建整个验证对象模型.)

public static class ValidationExtensions
{
  public static MvcHtmlString ValidationMessageHtmlFor<TModel,TProperty>(
    this HtmlHelper<TModel> htmlHelper,Expression<Func<TModel,TProperty>> expression)
  {
    return new MvcHtmlString(
      HttpUtility.HtmlDecode(
        htmlHelper.ValidationMessageFor<TModel,TProperty>(
        expression,null,((IDictionary<string,object>)new RouteValueDictionary()))
        .ToHtmlString()));
  }
}

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....