MVC3 远程验证未触发?
我创建了一个非常简单的测试项目来说明这个问题。
我的模型类
public class HomeModel
{
[Required(ErrorMessage="Missing property1.")]
public string Property1
{
get;
set;
}
[Remote("ValidateProperty2", "Home", HttpMethod="Get", AdditionalFields = "Property3", ErrorMessage="Property2 wrong!")]
public string Property2
{
get;
set;
}
public string Property3
{
get;
set;
}
}
我的控制器
public class HomeController : Controller
{
public ActionResult Index()
{
HomeModel model = new HomeModel();
return View(model);
}
[HttpPost]
public ActionResult Index(HomeModel model)
{
return View(model);
}
public ActionResult ValidateProperty2(string property2, string property3)
{
return Json(true, JsonRequestBehavior.AllowGet);
}
}
和我的视图
@model RemoteValidationTest.Models.HomeModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
</head>
<body>
@using (Ajax.BeginForm(new AjaxOptions
{
HttpMethod = "POST"
}))
{
@Html.TextBoxFor(x => x.Property1) @Html.ValidationMessageFor(x => x.Property1)<br />
@Html.TextBoxFor(x => x.Property2) @Html.ValidationMessageFor(x => x.Property2)<br />
@Html.TextBoxFor(x => x.Property3) @Html.ValidationMessageFor(x => x.Property3)<br />
<input type="submit" />
}
</body>
</html>
和我的 web.config
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
这里没有什么特别的。我有一个具有 3 个属性的模型类。第一个是必需的,第二个是远程验证,我相信我已经正确创建了操作方法。我将中断点设置为 ValidateProperty2 函数,它永远不会被调用。
我还使用了FireBug,同样的事情客户端甚至不会尝试调用服务器端。
这里的代码有什么问题吗?
编辑1: 我想我得到了一些东西,只有当控件(例如文本框)在侧面具有值时才会触发远程验证。空控件永远不会触发验证。就我而言,我实际上尝试实现更复杂的逻辑,即使控制文本为空(以检查其他属性的值),我也需要触发验证。有可能吗?
I created a very simple test project to illustrate the problem.
My model class
public class HomeModel
{
[Required(ErrorMessage="Missing property1.")]
public string Property1
{
get;
set;
}
[Remote("ValidateProperty2", "Home", HttpMethod="Get", AdditionalFields = "Property3", ErrorMessage="Property2 wrong!")]
public string Property2
{
get;
set;
}
public string Property3
{
get;
set;
}
}
My controller
public class HomeController : Controller
{
public ActionResult Index()
{
HomeModel model = new HomeModel();
return View(model);
}
[HttpPost]
public ActionResult Index(HomeModel model)
{
return View(model);
}
public ActionResult ValidateProperty2(string property2, string property3)
{
return Json(true, JsonRequestBehavior.AllowGet);
}
}
And my view
@model RemoteValidationTest.Models.HomeModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
</head>
<body>
@using (Ajax.BeginForm(new AjaxOptions
{
HttpMethod = "POST"
}))
{
@Html.TextBoxFor(x => x.Property1) @Html.ValidationMessageFor(x => x.Property1)<br />
@Html.TextBoxFor(x => x.Property2) @Html.ValidationMessageFor(x => x.Property2)<br />
@Html.TextBoxFor(x => x.Property3) @Html.ValidationMessageFor(x => x.Property3)<br />
<input type="submit" />
}
</body>
</html>
And my web.config
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
Nothing really fancy here. I havea model class with 3 properties. The first one is set be required and second one is remote validation, I believed I have create the action method properly. I set break piont to ValidateProperty2
function, it never gets called.
I also used FireBug, the same thing client side does not even try to call the server side.
What is wrong with the code here?
Edit 1:
I think I get something, the remote validation will only fires when the control (e.g text box) has value in side. The empty control will never trigger the validation. In my case I actually try to implmenet a more complicated logic, I need the validation to fire even when the control text is empty (to check other property's value). Is it even possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我这里有一个 mvc3 的工作版本:
http://completedevelopment.blogspot.com/2011/08 /remote-ajax-validation-in-mvc3.html
d/l 它,您可以比较文件。
I have a working version here for mvc3:
http://completedevelopment.blogspot.com/2011/08/remote-ajax-validation-in-mvc3.html
d/l it and you can compare files.
问题是,当您第一次按下发送按钮时,验证不会自动触发。以下是在页面加载时启动验证的方法,以便每次提交时需要验证时都会触发验证:
“必需”验证属性在 asp.net mvc 3 中不起作用,而其他属性则有效
The problem is that the validation don't fire automatically when you press the send button for the first time. Here is how you initiate the validation upon page load so that it fires every time it is needed on submit:
"Required" validation attribute not working in asp.net mvc 3 while others work