jquery - asp.net mvc 3 中的远程验证不起作用

标签 jquery asp.net-mvc validation asp.net-mvc-3 data-annotations

我正在尝试按照Here中的教程来实现远程验证但它在我的情况下不起作用我的代码如下 网络 session

<appSettings>
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    <add key="CrystalImageCleaner-AutoStart" value="true" />
    <add key="CrystalImageCleaner-Sleep" value="60000" />
    <add key="CrystalImageCleaner-Age" value="120000" />
  </appSettings>

网站管理员

<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.4.js" type="text/javascript"></script>
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.7/jquery.validate.js" type="text/javascript"></script>
   <script type="text/javascript" src="<%=Url.Content("~/Scripts/jquery.validate.unobtrusive.js")%>"></script>

查看

<div class="editor-field">
     <%= Html.TextBoxFor(model => model.CNIC)%>
      <%= Html.ValidationMessageFor(model => model.CNIC)%>
</div>

Controller

public ActionResult CheckDuplicate(string myvar)
        {
            return Json(!myvar.Equals("362-662-1"), JsonRequestBehavior.AllowGet);
        }

型号

[Remote("CheckDuplicate", "Home", "Already Exists")]

在 firebug 中,我得到以下输出,这与预期不同

 <input type="text" value="" name="uname" id="uname" data-val-required="This Field is Required" data-val="true">
while tutorial shows the following for its textbox
<input type="text" value="" name="UserName" id="UserName" data-val-required="The UserName field is required." data-val-remote-url="/Validation/IsUID_Available" data-val-remote-additionalfields="*.UserName" data-val-remote="&amp;#39;UserName&amp;#39; is invalid." data-val-regex-pattern="(\S)+" data-val-regex="White space is not allowed" data-val-length-min="3" data-val-length-max="6" data-val-length="The field UserName must be a string with a minimum length of 3 and a maximum length of 6." data-val="true" class="text-box single-line">

最佳答案

该属性应如下所示:

[Remote("CheckDuplicate", "Home", ErrorMessage = "Already Exists")]

如果您使用带有 3 个字符串参数的构造函数,它们对应于操作、 Controller 和区域。

型号:

public class MyViewModel
{
    [Remote("CheckDuplicate", "Home", ErrorMessage = "Already Exists")]
    public string CNIC { get; set; }
}

Controller :

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

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

    public ActionResult CheckDuplicate(string cnic)
    {
        return Json(!cnic.Equals("362-662-1"), JsonRequestBehavior.AllowGet);
    }
}

查看:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<AppName.Models.MyViewModel>" %>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.4.js" type="text/javascript"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.7/jquery.validate.js" type="text/javascript"></script>
<script type="text/javascript" src="<%=Url.Content("~/Scripts/jquery.validate.unobtrusive.js")%>"></script>

<% using (Html.BeginForm()) { %>
    <%= Html.TextBoxFor(model => model.CNIC)%>
    <%= Html.ValidationMessageFor(model => model.CNIC)%>    
    <input type="submit" value="OK" />
<% } %>

</asp:Content>

另请注意传递给 CheckDuplicate 操作的操作参数名称:它应该与模型属性的名称匹配。

关于jquery - asp.net mvc 3 中的远程验证不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5471397/

相关文章:

javascript - jquery 不会从 html 中删除元素

php - 如何通过ajax获取多个查询的结果

asp.net-mvc - ASP.NET MVC 预览版 5 - Html.Image 助手已移动命名空间

c# - 如何将 CausesValidation 设置为 false 以关闭 [Windows 窗体上的 Xbox?

javascript - 使用单选按钮根据表单输入转到 URL

jquery - 滚动 adsense 时的粘性菜单

asp.net mvc windows azure 迁移 System.BadImageFormatException : Could not load file or assembly 'Interop.SSCE' or

asp.net-mvc - 如何构建企业 MVC 应用程序,业务逻辑在哪里?

php - 在 MVC 网站中验证和处理表单提交的位置

jquery - 提交前如何验证表单