Asp.net mvc 将ViewModel验证消息添加到@HTML.LabelFor中的HTML属性
我想验证一个具有asp.net核心mvc内置功能的表单,但是验证消息应该与来自的数据错误属性一起很好地显示。要使其工作,我需要视图中的消息作为字符串 在视图中,我添加了Asp.net mvc 将ViewModel验证消息添加到@HTML.LabelFor中的HTML属性,asp.net-mvc,validation,asp.net-core,materialize,Asp.net Mvc,Validation,Asp.net Core,Materialize,我想验证一个具有asp.net核心mvc内置功能的表单,但是验证消息应该与来自的数据错误属性一起很好地显示。要使其工作,我需要视图中的消息作为字符串 在视图中,我添加了jquery.validate.js和jquery.validate.unobtrusive.js。两者都已正确加载。表单将与jquery ajax一起提交。为了进行验证,submit按钮执行一个AJAX调用,并阻止提交操作 通常情况下,我会在视图中编写类似的内容(有效): 但我想将验证消息设置为dataerrorhtml属性,使
jquery.validate.js
和jquery.validate.unobtrusive.js
。两者都已正确加载。表单将与jquery ajax一起提交。为了进行验证,submit按钮执行一个AJAX调用,并阻止提交操作
通常情况下,我会在视图中编写类似的内容(有效):
但我想将验证消息设置为dataerrorhtml属性,使其样式类似于materialiecss
所以我试着:
@Html.TextBoxFor(model => model.Email, "", new { @id = "EMail", @type = "email", @class = "validate" })
@Html.LabelFor("E-Mail Address", null, new { @for = "EMail", data_error = Html.ValidationMessageFor(model => model.Email) })
wich总是给我“Microsoft.AspNetCore.Mvc.Rendering.TagBuilder”作为错误消息
如何将ViewModel中的验证消息设置为@html.LabelForModule
方法中的数据错误
html属性?
要清除,这是我的ViewModel:
public class UserViewModel
{
[Required(ErrorMessage = "The FirstName address is required")]
public string FirstName { get; set; }
[Required(ErrorMessage = "The LastName address is required")]
public string LastName { get; set; }
[Required(ErrorMessage = "The Email address is required")]
[EmailAddress(ErrorMessage = "Invalid Email Address")]
public string Email { get; set; }
}
您可以通过css显示错误消息,如
materialiecss
,而不显示数据错误属性
HTML输入:
<div class="input-field col s12">
@Html.TextBoxFor(model => model.Email, "", new { @id = "EMail", @type = "email",
@class = "validate" })
@Html.LabelFor("E-Mail Address", null, new { @for = "EMail"})
@Html.ValidationMessageFor(model => model.Email)
</div>
我不确定这是否是您想要的,但不久前我有一个类似的(我相信)问题。这是我得到的答案。
<div class="input-field col s12">
@Html.TextBoxFor(model => model.Email, "", new { @id = "EMail", @type = "email",
@class = "validate" })
@Html.LabelFor("E-Mail Address", null, new { @for = "EMail"})
@Html.ValidationMessageFor(model => model.Email)
</div>
.field-validation-error span {
color: inherit;
display: block;
position: absolute;
right: 0;
top: 100%;
transition: .2s opacity ease-out, .2s color ease-out;
-webkit-transition: .2s opacity ease-out, .2s color ease-out;
}