客户端验证正在触发,但不显示消息

时间:2016-06-10 18:19:22

标签: jquery html asp.net-mvc asp.net-mvc-5

对于电子邮件字段,我有这个生成的HTML

<input name="PasswordLoserEmail" class="input-validation-error form-control" id="PasswordLoserEmail" aria-invalid="true" aria-required="true" aria-describedby="PasswordLoserEmail-error" type="email" value="" data-val-required="The email address is required" data-val="true" data-val-email="Invalid Email Address">

从这个Razor代码:

@Html.TextBoxFor(m => m.PasswordLoserEmail, new { @class = "form-control", type = "email" })

我添加的注释是:

[Display(Name = "Email address")]
[EmailAddress(ErrorMessage = "Invalid Email Address")]
[Required(ErrorMessage = "The email address is required")]
public string PasswordLoserEmail { get; set; }

我的HTML页面底部也有这个:

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

并且刚刚将此内容添加到我的CSS

.input-validation-error {
    display: block;
    color: red;
}

但是当我输入无效的电子邮件地址时,虽然它阻止了表单的提交,但它没有向用户显示错误消息。 为什么?

1 个答案:

答案 0 :(得分:2)

您需要添加ValidationMessageFor以显示错误消息:

@Html.ValidationMessageFor(m => m.PasswordLoserEmail)