asp.net mvc 3 中的远程验证不起作用
我正在尝试按照 中的教程来实现远程验证这里但在我的情况下不起作用我的代码如下 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="&#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">
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="'UserName' 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该属性应如下所示:
如果您使用带有 3 个字符串参数的构造函数,它们对应于操作、控制器和区域。
模型:
控制器:
视图:
另请注意传递给
CheckDuplicate
操作的操作参数名称:它应该与模型属性的名称匹配。The attribute should look like this:
If you use the constructor with 3 string arguments they correspond to action, controller and area.
Model:
Controller:
View:
Also notice the name of the action argument passed to
CheckDuplicate
action: it should match the name of the model property.