刚刚开始使用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())); } }