MVC 4客户端验证显示错误消息 [英] MVC 4 Client Side Validation Showing Wrong Message

查看:95
本文介绍了MVC 4客户端验证显示错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用客户端验证的MVC 4应用程序。我正在努力的表单有多个必填字段,但我得到每个字段的相同验证消息。



Razor代码如下所示:



I have an MVC 4 application using client-side validation. The form I am struggling with has multiple required fields, but Iam getting the same validation message for each field.

The Razor code looks like this:

<section class="input-label-group">
    @Html.Html5TextBoxFor(m => m.Email, InputTypes.InputType.Email, htmlAttributes: new { autofocus = "autofocus", autocomplete = "off"})
    @Html.ValidationMessageFor(m => m.Email)
</section>
<section class="input-label-group">
    @Html.Html5TextBoxFor(m => m.FullName, InputTypes.InputType.Text, htmlAttributes: new { autocomplete = "off" })
    @Html.ValidationMessageFor(m => m.FullName)
</section>



模型看起来像这样:




The model looks like this:

[Display(Name = "Email")]
[Required(ErrorMessage = "Please enter a valid email address")]
[StringLength(128, ErrorMessage = "The email address may not exceed 128 characters")]
public string Email { get; set; }

[Display(Name = "Full Name")]
[Required(ErrorMessage = "Please enter the user's full name")]
[StringLength(255, ErrorMessage = "The user's name may not exceed 255 characters")]
public string FullName { get; set; }



但是看看服务器在初始页面加载时的响应(即在任何javascript运行之前),我得到以下结果:




But looking at the response from the server on initial page load (i.e. before any javascript has run), I get the following:

<section class="input-label-group">
    <input autocomplete="off" autofocus="autofocus" data-val="true" data-val-length="The email address may not exceed 128 characters" data-val-length-max="128" data-val-required="Please enter a valid email address" id="Email" name="Email" placeholder="Email" type="email" value="" />
    <span class="field-validation-valid" data-valmsg-for="Email" data-valmsg-replace="true"></span>
</section>
<section class="input-label-group">
    <input autocomplete="off" data-val="true" data-val-length="The email address may not exceed 128 characters" data-val-length-max="128" data-val-required="Please enter a valid email address" id="FullName" name="FullName" placeholder="Full Name" type="text" value="" />
    <span class="field-validation-valid" data-valmsg-for="FullName" data-valmsg-replace="true"></span>
</section>



注意第二个控件的数据属性是指电子邮件而不是全名。当提交空字段时,这会导致最终形式的验证消息(使用jquery.validate.js和jquery.validate.unobtrusive.js):



不正确的验证消息


Notice the data attributes for the second control refer to email not full name. This causes the validation messages in the final form (using jquery.validate.js and jquery.validate.unobtrusive.js) when submitting empty fields to look like this:

Incorrect Validation Message

推荐答案

一切看起来都正确,我认为没有理由为什么初始页面加载会像你在这里展示的那样呈现。



我很确定这是某种错误/意外行为。你可以尝试相同的东西,但没有[Required(ErrorMessage =请输入用户的全名)]中的撇号?另外,您使用的是jquery.validate.js和jquery.validate.unobtrusive.js的兼容/最新版本吗?
Everything looks correct and I see no reason why initial page load would render like you showed here.

I am pretty sure that this is some sort of bug/unexpected behavior. Can you try the same thing but without the apostrophe in [Required(ErrorMessage = "Please enter the user's full name")]? Also, are you using compatible/uptodate versions of jquery.validate.js and jquery.validate.unobtrusive.js?


这篇关于MVC 4客户端验证显示错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆