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

发布于 2024-10-27 07:14:38 字数 2237 浏览 0 评论 0原文

我正在尝试按照 中的教程来实现远程验证这里但在我的情况下不起作用我的代码如下 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">

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

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

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

 <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="&#39;UserName&#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">

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡写薰衣草的香 2024-11-03 07:14:38

该属性应如下所示:

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

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

模型:

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

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文