2

我试图从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> 

网站。掌握

<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我得到下面的输出是从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="&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"> 
+0

相似的问题是另一个页面: http://stackoverflow.com/a/37366502/4146766 – 2016-05-21 18:40:20

回答

2

属性的不同应该是这样的:

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

如果您使用constructo r带有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动作的动作参数的名称:它应该匹配的名称模型属性。

+0

我已经将我的应用程序从mvc 2转换为mvc 3这可能会导致一些问题? – Tassadaque 2011-03-29 11:37:41

+0

@Tassadaque,你是否执行过我在我的回答中建议的更改? – 2011-03-29 11:42:07

+0

我看到只有一个变化“ErrorMessage =”,我做了,但没有运气! – Tassadaque 2011-03-29 11:55:20