@使用jquery Ajax的html.BeginForm不会';我不能完全工作
我必须在公式上使用Ajax和asp.NETMVC。我的经理不希望我使用@Ajax.BeginForm,因此我必须将Ajax与Jquery方法结合使用。 我的问题是,甚至我的代码似乎都能工作,当它进入控制器时,控制器返回一个新页面(就像表单是用一篇简单的帖子提交的),因此我没有ajax的优势。我真的不知道我在哪里犯了错误,也不知道我是否做错了。这是我的代码: HTML: 控制器:@使用jquery Ajax的html.BeginForm不会';我不能完全工作,jquery,asp.net,ajax,Jquery,Asp.net,Ajax,我必须在公式上使用Ajax和asp.NETMVC。我的经理不希望我使用@Ajax.BeginForm,因此我必须将Ajax与Jquery方法结合使用。 我的问题是,甚至我的代码似乎都能工作,当它进入控制器时,控制器返回一个新页面(就像表单是用一篇简单的帖子提交的),因此我没有ajax的优势。我真的不知道我在哪里犯了错误,也不知道我是否做错了。这是我的代码: HTML: 控制器: [HttpPost] public ActionResult AddCtrl(GroupViewMod
[HttpPost]
public ActionResult AddCtrl(GroupViewModels grp)
{
System.Diagnostics.Debug.WriteLine("YOOOOOOOOOOOOOOOOOOOOO");
System.Diagnostics.Debug.WriteLine(grp.group.Name);
return Json("{yo:toto}");
}
在表单提交中使用ajax时,需要使用
e.preventDefault()
而不是returnfalse
试试这个:
$(function () {
$('#addGroupForm').submit(function (e) {
if ($(this).valid()) {
//the action is send with this ajax request and the send works
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
success: function (result) {
alert("yo");
}
});
}
e.preventDefault();
});
});
检查脚本错误。如果在返回false之前有任何错误,则不会阻止重定向
event.preventDefault()
在这方面更好,因为您将它放在提交处理程序的顶部。
[HttpPost]
public ActionResult AddCtrl(GroupViewModels grp)
{
System.Diagnostics.Debug.WriteLine("YOOOOOOOOOOOOOOOOOOOOO");
System.Diagnostics.Debug.WriteLine(grp.group.Name);
return Json("{yo:toto}");
}
$(function () {
$('#addGroupForm').submit(function (e) {
if ($(this).valid()) {
//the action is send with this ajax request and the send works
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
success: function (result) {
alert("yo");
}
});
}
e.preventDefault();
});
});