MVC Core输入字段验证,带HTML标记的自定义错误消息

问题描述 投票:0回答:1

我正在使用ASP.net MVC Core 2,我有一个自定义错误消息的必需属性(来自DataAnnotations)。

如果邮件是纯文本,它可以正常工作。例如:

[Required (ErrorMessage = "My custom error message")]
public string UserName { get; set; }

但我希望在出错的情况下显示:

<span class="fas fa-exclamation-triangle"></span> My custom error message

put,消息的html标签部分不起作用,它只是将其显示为文本

[Required (ErrorMessage = "<span class=\"fas fa-exclamation-triangle\"></span>My custom error message")]
public string UserName { get; set; }
asp.net-core-mvc data-annotations
1个回答
0
投票

如果一切顺利,并且您在问题中显示的model页面中导入了正确的View,那么只需执行以下操作,

<label asp-for="UserName" class="control-label"></label>
<input asp-for="UserName" class="form-control" />
// style your span as you like to.
<span asp-validation-for="UserName" class="text-danger"></span>
© www.soinside.com 2019 - 2024. All rights reserved.