asp.net mvc 3中的远程验证无效

时间:2021-11-08 17:20:03

I am trying to implement remote validation by following tutorial from Here but it is not working in my case My code as follows Web.Conf

我正在尝试通过下面的教程来实现远程验证,但是在我的例子中,我的代码不像下面的Web.Conf

<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>

Site.Master

Site.Master

<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>

View

视图

<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);
        }

Model

模型

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

In firebug i get the following output which is different from exptected

在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">

1 个解决方案

#1


2  

The attribute should look like this:

属性应该如下所示:

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

If you use the constructor with 3 string arguments they correspond to action, controller and area.

如果你使用的构造函数有3个字符串参数,它们对应的动作,控制器和区域。

Model:

模型:

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);
    }
}

View:

观点:

<%@ 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>

Also notice the name of the action argument passed to CheckDuplicate action: it should match the name of the model property.

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

#1


2  

The attribute should look like this:

属性应该如下所示:

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

If you use the constructor with 3 string arguments they correspond to action, controller and area.

如果你使用的构造函数有3个字符串参数,它们对应的动作,控制器和区域。

Model:

模型:

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);
    }
}

View:

观点:

<%@ 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>

Also notice the name of the action argument passed to CheckDuplicate action: it should match the name of the model property.

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