asp.net中的远程验证mvc 3无法正常工作

时间:2011-03-29 10:57:03

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

我正在尝试按照Here中的教程实现远程验证,但它不适合我的情况我的代码如下 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

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

控制器

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

1 个答案:

答案 0 :(得分:2)

该属性应如下所示:

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

如果你使用带有3个字符串参数的构造函数,它们对应于action,controller和area。

型号:

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

控制器:

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操作的操作参数的名称:它应该与模型属性的名称匹配。