我正在使用MVC 3中的远程验证属性来检查电子邮件地址的唯一性,并且在使用ajax表单时我无法触发它。我有一个表单_SignupForm,我在两个场景中使用:1。当启用javascript时作为jquery对话框,以及2.禁用javascript时的常规页面(也可通过浏览页面获得)。
当我导航到正常的cshtml页面时,所有验证都按预期工作。当我在jquery对话框中使用表单时,除远程验证外,所有验证都有效。
这是我的模特:
public class SignUpModel : BaseModel
{
[Display(Name = "Your Name")]
[Required(AllowEmptyStrings = false, ErrorMessage = "Please enter your name")]
public string Name { get; set; }
[Required(AllowEmptyStrings = false, ErrorMessage = "Please enter your email address")]
[DataType(DataType.EmailAddress, ErrorMessage = "Please enter a valid email address")]
[Remote("EmailAddress", "Validation", ErrorMessage = "Account already exists", HttpMethod = "POST")]
[Display(Name = "Your Email")]
public string EmailAddress { get; set; }
[Required(AllowEmptyStrings = false, ErrorMessage = "Please enter a password")]
[DataType(DataType.Password)]
[Display(Name = "Choose a password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Retype password")]
[Compare("Password", ErrorMessage = "Your passwords do not match")]
public string ConfirmPassword { get; set; }
[Display(Name = "Receive a weekly newsletter")]
public bool ReceiveNewsletter { get; set; }
public bool Agree { get; set; }
这是我的表格:
@using (Ajax.BeginForm(new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "signupDialog" }))
{
@Html.ValidationSummary(true)
<ol>
@*name*@
<li>
<div class="label">
@Html.LabelFor(m => m.Name)
</div>
<div class="field">
@Html.TextBoxFor(m => m.Name)
@Html.ValidationMessageFor(m => m.Name)
</div>
</li>
@*email*@
<li>
<div class="label">
@Html.LabelFor(m => m.EmailAddress)
</div>
<div class="field">
@Html.TextBoxFor(m => m.EmailAddress)
@Html.ValidationMessageFor(m => m.EmailAddress)
</div>
</li>
@*password*@
<li>
<div class="label">
@Html.LabelFor(m => m.Password)
</div>
<div class="field">
@Html.TextBoxFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
</div>
</li>
@*confirm password*@
<li>
<div class="label">
@Html.LabelFor(m => m.ConfirmPassword)
</div>
<div class="field">
@Html.TextBoxFor(m => m.ConfirmPassword)
@Html.ValidationMessageFor(m => m.ConfirmPassword)
</div>
</li>
@*newsletter*@
<li>
<div class="field">
@Html.CheckBoxFor(m => m.ReceiveNewsletter)
</div>
<div class="label">
@Html.LabelFor(m => m.ReceiveNewsletter)
</div>
</li>
@*terms*@
<li>
<div class="field">
@Html.CheckBoxFor(m => m.Agree)
</div>
<div class="label">
@Html.LabelFor(m => m.Agree)
</div>
</li>
<li>
<input type="submit" value="Save" />
</li>
</ol>
}
当我使用萤火虫检查田地时,这是我在两种情况下都看到的:
<input id="EmailAddress" type="text" value="" name="EmailAddress" data-val-required="Please enter your email address" data-val-remote-url="/garden/Validation/EmailAddress" data-val-remote-type="POST" data-val-remote-additionalfields="*.EmailAddress,*.UserId" data-val-remote="Account already exists" data-val="true">
提前感谢您的帮助。
修改 经过一番挖掘后,似乎客户端验证在我的jquery对话框中根本不起作用。这甚至可能吗?
答案 0 :(得分:1)
我在MVC3中遇到了与远程验证相同的问题。在我的例子中,我使用JQuery.load()方法在JQuery.dialog()内部动态加载表单。
我可以通过在加载表单后通过调用以下内容来设置客户端验证工作,只要我设置了unobstrusive和clientvalidation的标志以及相应的js文件:
$.validator.unobtrusive.parse($("form"));
然后,在我的$ .ajax调用发布结果之前,我检查表单是否有效:
if ($(form).valid()) {
这会导致客户端触发并向我提供客户端验证。但是,当发生这种情况时,它还会触发MVC远程验证代码但是空值,导致远程验证方法失败,因为没有什么可比较的。