Asp.net mvc 2 JQuery验证的远程验证规则失败(使用MVC2)
我的标记中有一个字段,我想向服务器验证该字段。我的表单当前如下所示:Asp.net mvc 2 JQuery验证的远程验证规则失败(使用MVC2),asp.net-mvc-2,jquery,jquery-validate,Asp.net Mvc 2,Jquery,Jquery Validate,我的标记中有一个字段,我想向服务器验证该字段。我的表单当前如下所示: <% using (Html.BeginForm()){ %> <%: Html.Serialize("regData", Model)%> <div class="RegistrationGroup"> <p><label for="Account_Email">e-mail</label> <%: Html.EditorFor(x
<% using (Html.BeginForm()){ %>
<%: Html.Serialize("regData", Model)%>
<div class="RegistrationGroup">
<p><label for="Account_Email">e-mail</label> <%: Html.EditorFor(x => x.Account.Email) %><span class="ErrorMessage"></span></p>
</div>
<% } %>
在我的RegistrationController类中,我有以下方法:
public string ValidateEmail(string email)
{
if (email.Contains("oek"))
return "false";
return "true";
}
因此,对于测试,如果电子邮件地址中的某个地方有“oek”
,则应返回false,并使验证失败
然而,这是行不通的
如果没有远程部分,所需的和电子邮件规则将正常工作,因此我知道它们正在工作
添加远程规则时,将执行以下步骤:
- 控制器上的方法按计划调用,其参数为电子邮件地址
- 它在浏览器中截获的方法的返回值分别为true或false(我使用Chrome的开发者工具来控制这一点)
public ActionResult ValidateEmail(string email)
{
if ((email ?? string.Empty).Contains("oek"))
{
return Json(false, JsonRequestBehavior.AllowGet);
}
return Json(true, JsonRequestBehavior.AllowGet);
}
此外,您在电子邮件:true
规则后缺少逗号:
$('form').validate({
rules: {
'Account.Email': {
required: true,
email: true,
remote: '@Url.Action("ValidateEmail", "Registration")'
}
}
});
或者,如果这是一个单独的javascript文件,您可以在电子邮件字段中使用HTML5data-*
属性:
$('form').validate({
rules: {
'Account.Email': {
required: true,
email: true,
remote: $('#Account_Email').data('remote-val-url')
}
}
});
更新: 完整的工作示例。确保电子邮件操作参数在
ValidateEmail
操作中正确绑定
型号:
public class MyViewModel
{
public MyViewModel()
{
Account = new Account();
}
public Account Account { get; set; }
}
public class Account
{
public string Email { get; set; }
}
控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new MyViewModel());
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
return View(model);
}
public ActionResult ValidateEmail(Account account)
{
if ((account.Email ?? string.Empty).Contains("oek"))
{
return Json(false, JsonRequestBehavior.AllowGet);
}
return Json(true, JsonRequestBehavior.AllowGet);
}
}
视图:
电子邮件
x、 帐户(电子邮件)%>
$('form')。请验证({
规则:{
“Account.Email”:{
要求:正确,
电子邮件:是的,
远程:“”
}
}
});
我终于在这里找到了答案,错的不是我的代码,而是jQuery.Validate插件版本
我使用了VisualStudio2010附带的验证插件,并在同一个项目中将jQuery从1.4.1版升级到1.5.1版。问题在于Visual Studio 2010(1.6版)附带的验证插件与jQuery 1.5.1不兼容。它主要是,但不是基于Ajax的功能
因此,将jQuery.Validate更新为1.8解决了问题。@Darin-这对我不起作用。我将ValidateEmail更改为您上面提到的returntype,但在Chrome中似乎返回了相同的结果。因此,作为一个普通字符串,要么为true,要么为false。我忘记了逗号,因为远程行是在我的代码上注释的,但是捕捉得很好:)。但是,将远程调用更改为
@Url.Action…
会导致根本不会调用服务器方法,因此我必须将其还原为“Registration/ValidateEmail”
。那不管用吗?最终结果是一样的。方法返回true或false,但字段验证失败。是不是我使用的“Registration/ValidateEmail”
导致它失败(即使我可以看到该方法正在运行,并将其值返回到浏览器?这一天一直困扰着我:(@Øyvind Knobloch Brå然后,请查看我的更新以获取完整示例。在您的ValidateEmail
操作中接收电子邮件值的方式可能存在问题。因为插件将以ValidateEmail?帐户的形式发送。email=xxx
您需要执行正确的模型绑定。@Darin-非常感谢您的努力,但是sult是一样的。我将ValidateEmail改为使用ViewModel,调试时我可以看到它接收电子邮件,并分别返回true或false。只是浏览器中的验证似乎根本不关心结果。感谢您花时间帮助我。我想我将不得不求助于编写custom验证器方法,但当它本应在开箱即用时,它感觉没有必要。再次感谢您的时间。@Darin-我现在找到了我问题的答案,如果您好奇的话,我已经将其添加为这个问题的答案。很抱歉浪费您的时间。
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new MyViewModel());
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
return View(model);
}
public ActionResult ValidateEmail(Account account)
{
if ((account.Email ?? string.Empty).Contains("oek"))
{
return Json(false, JsonRequestBehavior.AllowGet);
}
return Json(true, JsonRequestBehavior.AllowGet);
}
}
<script src="<%= Url.Content("~/Scripts/jquery.validate.js") %>" type="text/javascript"></script>
<% using (Html.BeginForm()) { %>
<label for="Account_Email">e-mail</label>
<%= Html.EditorFor(x => x.Account.Email) %>
<span class="ErrorMessage"></span>
<% } %>
<script type="text/javascript">
$('form').validate({
rules: {
'Account.Email': {
required: true,
email: true,
remote: '<%= Url.Action("ValidateEmail", "Home") %>'
}
}
});
</script>