我有一个使用客户端验证的MVC 4应用程序。我正在努力的表单有多个必填字段,但我为每个字段获得相同的验证消息。
Razor代码如下所示:
<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>
该模型如下所示:
[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运行之前),我得到以下内容:
<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):
我一定错过了一些明显的想法吗?