我正在使用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; }
如果一切顺利,并且您在问题中显示的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>