我有一个更新订单的视图,其中包含一些字段和两个按钮(提交按钮和后退按钮)
我使用jquery ajax将参数从View发送到控制器:
function onUpdate(e) {
var data = getParameters()
$.ajax({
url: '@Url.Action("UpdateOrder", "Home")',
type: 'post',
dataType: 'json',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(data),
success: function () {
}
});
};
我的控制员:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult UpdateOrder(int orderId,int provinceId,int paymentType,int totalAmount,string isClosed)
{
Order order = new Order();
order.OrderId = orderId;
order.ProvinceId = provinceId;
order.PaymentTypeId = paymentType;
order.TotalAmount = totalAmount;
order.IsClosed = isClosed=="True"?true:false;
order.UpdateOrder(order);
return RedirectToAction("Default");
}
当我点击提交时,记录更新,但不会重定向到默认操作。
答案 0 :(得分:2)
不是从httppost控制器重定向,而是尝试: -
function onUpdate(e) {
var data = getParameters()
$.ajax({
url: '@Url.Action("UpdateOrder", "Home")',
type: 'post',
dataType: 'json',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(data),
success: function () {
window.location.href="/yourcontroller/Default"; //<------redirect here
}
});
};
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult UpdateOrder(int orderId,int provinceId,int paymentType,int totalAmount,string isClosed)
{
Order order = new Order();
order.OrderId = orderId;
order.ProvinceId = provinceId;
order.PaymentTypeId = paymentType;
order.TotalAmount = totalAmount;
order.IsClosed = isClosed=="True"?true:false;
order.UpdateOrder(order);
return Json(new{}, JsonRequestBehavior.AllowGet); // <------ change here
}
或者只是不用从jquery ajax调用HttpPost控制器,你可以使用表单提交执行普通帖子,正常帖子可以在任何你想要的地方重定向。