【发布时间】:2021-12-28 06:51:52
【问题描述】:
我有这个控制器动作:
[HttpPost]
public ActionResult OrderData(Order order)
{
var result = new { redirectToUrl = Url.Action("SeatSelection", "Orders", new { id = order.ScreeningId }), order };
return Json(result);
}
我正在尝试将订单对象传递给另一个操作:
public ActionResult SeatSelection(int id, Order order)
{
var screeningInDb = _context.Screenings.Include(s => s.Seats).Single(s => s.Id == order.ScreeningId);
var viewModel = new SeatSelectionViewModel
{
Seats = screeningInDb.Seats,
NumberOfTicketsOrdered = order.NumberOfTicketsOrdered
};
return View("SeatSelection", viewModel);
}
问题是 - 我在SeatSelectionAction 中收到的唯一参数是 id 参数,尽管OrderDataAction 中的订单对象是有效的。我很确定问题出在我试图传递订单对象的方式之内,也许是语法问题?
这是我将表单数据发布到 OrderData 操作的方式:
$.ajax({
type: "POST",
url: '@Url.Action("OrderData", "Orders")',
contentType: "application/json; charset=utf-8",
data: JSON.stringify(orderData),
dataType: "json",
success: function (res) {
alert("Success!");
window.location.href = res.redirectToUrl;
},
error: function (xhr, status, error) {
alert(status);
}
});
底线 - 我最终要做的是将表单传递给将处理数据的控制器操作,然后将新数据传递给“SeatSelection”视图。由于我的 post 方法发送 JSON 数据,因此我在执行此操作时遇到了麻烦,所以如果有更好的方法来做我想做的事情,我会很乐意学习!
【问题讨论】:
标签: c# json ajax asp.net-mvc model-view-controller