我在MVC中使用bootstrap而我试图使用验证但是我遇到了一个奇怪的问题。当我打开带有bootstrap stying的表单时,文本输入已经用红色标出,并且验证消息显示之前我已经在文本框中输入任何内容,有人能告诉我如何避免这种情况吗?继承人html
<div class="form-group has-feedback has-error">
<div class="col-md-4">
@Html.LabelFor(model => model.Contact.LastName, htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-6">
@Html.TextBoxFor(model => model.Contact.LastName, new { @class = "form-control", @placeholder = "Last Name" })
@Html.ValidationMessageFor(model => model.Contact.LastName, "You must include a last name.", new { @class = "text-danger" })
</div>
</div>
答案 0 :(得分:0)
你的语法:
@Html.ValidationMessageFor(model => model.Contact.LastName, "You must include a last name.", new { @class = "text-danger" })
在此您已经提供了错误消息=&#34;您必须包含姓氏。&#34; ..但它的消息来自 数据注释模型验证 < / strong>即可。
[Required(ErrorMessage = "You must include a last name.")]
public string LastName{ get; set; }
@Html.ValidationMessageFor(model => model.Contact.LastName, string.Empty, new { @class = "text-danger" })
在ValidationMessageFor中,消息应为null。如上面的代码所示。