gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 22:52:29 25 4
gpt4 key购买 nike

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

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

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

谢谢

最佳答案

这是使用 HtmlDecode() 反转编码的短期修复。为我工作。

(不必费心重建整个 Validation 对象模型。)

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()));
}
}

关于ASP.NET MVC - 将 XHTML 添加到验证错误消息中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2487087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com