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

如何解决用于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,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,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可解决此问题。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?