用于JQuery验证的远程验证规则失败(使用MVC 2)

| 我的标记中有一个要向服务器验证的字段。我的表单当前如下所示:
<% using (Html.BeginForm()){ %>
  <%: Html.Serialize(\"regData\", Model)%>
  <div class=\"RegistrationGroup\">
    <p><label for=\"Account_Email\">e-mail</label> <%: Html.EditorFor(x => x.Account.Email) %><span class=\"ErrorMessage\"></span></p>
  </div>
<% } %>
然后在我的jQuery脚本中,我有以下代码:
$(\"form\").validate({
  rules: {
    \"Account.Email\": {
      required: true,
      email: true
      remote: \"Registration/ValidateEmail\"
    }
  }
});
在我的RegistrationController类中,我具有以下方法:
public string ValidateEmail(string email)
{
  if (email.Contains(\"oek\"))
    return \"false\";
  return \"true\";
}
因此,对于测试,这应该返回false,并且如果电子邮件地址中的某处有“ 3”,则验证失败。 但是,这不起作用。 如果没有远程部分,则必需规则和电子邮件规则会按预期运行,因此我知道它们正在运行。 当我添加远程规则时,将发生以下步骤: 控制器上的方法按计划被调用,电子邮件地址作为参数。 从浏览器内部截取的方法返回的值分别为true或false(我使用Chrome \的开发人员工具来控制此值)。 但是,此时的验证无法按计划进行,并且在完成远程呼叫后,所需规则和电子邮件规则也将停止工作。 (在其他规则通过验证之前,它不会尝试强制执行远程规则,这当然是一件好事)。 我也尝试过返回带有true或false的JsonResult,但无济于事。 我在这里做错了什么?     
已邀请:
public ActionResult ValidateEmail(string email)
{
    if ((email ?? string.Empty).Contains(\"oek\"))
    {
        return Json(false, JsonRequestBehavior.AllowGet);
    }
    return Json(true, JsonRequestBehavior.AllowGet);
}
同样,您在遵循“ 5”规则后仍缺少逗号:
$(\'form\').validate({
    rules: {
        \'Account.Email\': {
            required: true,
            email: true,
            remote: \'@Url.Action(\"ValidateEmail\", \"Registration\")\'
        }
    }
});
或者,如果这是一个单独的javascript文件,则可以在电子邮件字段上使用HTML5
data-*
属性:
$(\'form\').validate({
    rules: {
        \'Account.Email\': {
            required: true,
            email: true,
            remote: $(\'#Account_Email\').data(\'remote-val-url\')
        }
    }
});
更新: 完整的工作示例。确保在“ 9”操作中正确绑定了电子邮件操作参数。 模型:
public class MyViewModel
{
    public MyViewModel()
    {
        Account = new Account();
    }
    public Account Account { get; set; }
}

public class Account
{
    public string Email { get; set; }
}
控制器:
public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new MyViewModel());
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        return View(model);
    }

    public ActionResult ValidateEmail(Account account)
    {
        if ((account.Email ?? string.Empty).Contains(\"oek\"))
        {
            return Json(false, JsonRequestBehavior.AllowGet);
        }
        return Json(true, JsonRequestBehavior.AllowGet);
    }
}
视图:
<script src=\"<%= Url.Content(\"~/Scripts/jquery.validate.js\") %>\" type=\"text/javascript\"></script>

<% using (Html.BeginForm()) { %>
    <label for=\"Account_Email\">e-mail</label> 
    <%= Html.EditorFor(x => x.Account.Email) %>
    <span class=\"ErrorMessage\"></span>
<% } %>


<script type=\"text/javascript\">
    $(\'form\').validate({
        rules: {
            \'Account.Email\': {
                required: true,
                email: true,
                remote: \'<%= Url.Action(\"ValidateEmail\", \"Home\") %>\'
            }
        }
    });
</script>
    
我终于在这里找到了答案,这不是我的代码错了,而是jQuery.Validate插件版本。 我使用了Visual Studio 2010附带的Validate插件,并且在同一项目中将jQuery从1.4.1版本升级到1.5.1。问题是Visual Studio 2010(版本1.6)附带的Validate插件与jQuery 1.5.1不兼容。它主要是但不是基于Ajax的功能。 因此,将jQuery.Validate更新为1.8可解决此问题。     

要回复问题请先登录注册