我有一个视图,它使用 ValidationMessageFor 访问模型中包含的数据注释,并且没有问题。但是,我不想在相关控件之外显示验证消息(作为标准做法),而是在用户将鼠标悬停在验证警告图标上时显示验证消息。
我已经设法让这部分工作,但它需要在用户单击验证警告图标之前有问题的控件具有焦点,否则它不会显示任何内容。是否可以通过 jQuery 检索验证消息?
模型:
[Required(ErrorMessageResourceName = "EmailRequiredErrorMessage", ErrorMessageResourceType = typeof(Resources.Languages.Models.RegistrationValidateModel))]
[StringLength(60, ErrorMessageResourceName = "EmailStringLengthErrorMessage", ErrorMessageResourceType = typeof(Resources.Languages.Models.RegistrationValidateModel))]
[Display(Name = "EmailDisplayName", ResourceType = typeof(Resources.Languages.Models.RegistrationValidateModel))]
[RegularExpression(@".+@.+\..+", ErrorMessageResourceName = "EmailInvalidRegularExpressionErrorMessage", ErrorMessageResourceType = typeof(Resources.Languages.Models.RegistrationValidateModel))]
public string Email { get; set; }
看法
@Html.ValidationMessageFor(m => m.Email)
生成的 HTML
<input class="span12" data-val="true" data-val-length="You must enter an email address less than 60 characters long" data-val-length-max="60" data-val-regex="You must enter a valid email address" data-val-regex-pattern=".+@.+\..+" data-val-required="You must enter an email address1" id="Email" name="Email" type="text" value="" />
...
<span class="field-validation-valid" data-valmsg-for="Email" data-valmsg-replace="true"></span>
目前,验证后悬停无法检索错误消息
但是,如果您给控件焦点,然后单击验证警告图标,它会显示消息(尽管位置略有不同):