繁体   English   中英

无法将 JSON 数据发送到 asp.net MVC controller 中的另一个操作

[英]Trouble sending JSON data to another action in asp.net MVC controller

我有这个 controller 动作:

[HttpPost]
        public ActionResult OrderData(Order order)
        {
            var result = new { redirectToUrl = Url.Action("SeatSelection", "Orders", new { id = order.ScreeningId }), order };

            return Json(result);
        }

我正在尝试将 object 订单传递给另一个操作:

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);
        }

问题是 - 我在SeatSelection Action 中收到的唯一参数是 id 参数,尽管OrderData Action 中的订单 object 是有效的。 我很确定问题出在我试图传递订单 object 的方式之内,也许是语法问题?

这是我将表单数据发布到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);
                    }
                });

底线 - 我最终要做的是将表单传递给 Controller 操作,在该操作中将处理数据,然后将新数据传递给“SeatSelection”视图。 我在执行此操作时遇到了麻烦,因为我的 post 方法发送 JSON 数据,所以如果有更好的方法来做我想做的事情,我会很乐意学习!

您的 model 与 SeatSelection 参数签名不匹配。

尝试:

$.ajax({
                type: "POST",
                url: '@Url.Action("OrderData", "Orders")',
                contentType: "application/json; charset=utf-8",
                data: `{"order": ${JSON.stringify(orderData)}}`,
                dataType: "json",
                success: function (res) {
                    alert("Success!");
                    window.location.href = res.redirectToUrl;
                },
                error: function (xhr, status, error) {
                    alert(status);
                }
            });

或者(这个只是创建一个 javascript object,其中有两个签名属性):

const sendObj = { id: 0, order: orderData };
$.ajax({
                type: "POST",
                url: '@Url.Action("OrderData", "Orders")',
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(sendObj),
                dataType: "json",
                success: function (res) {
                    alert("Success!");
                    window.location.href = res.redirectToUrl;
                },
                error: function (xhr, status, error) {
                    alert(status);
                }
            });

你不能用

 window.location.href = ...

因为在这种情况下,浏览器总是调用 GET 方法,该方法只能将数据保存在带有原始参数的查询字符串中,并且不会将 Order 转换为 qusery 字符串参数。 这就是为什么你只能得到 id。

在您的情况下,直接在操作中重定向会容易得多

 public ActionResult OrderData(Order order)
{
  return RedirectToAction( ( "SeatSelection", "Orders", new { id = order.ScreeningId }), order });

}

或者当它是相同的 controller 我通常这样做

public ActionResult OrderData(Order order)
{
    return SeatSelection (order.ScreeningId, order };

}

但由于您使用的是 ajax 它会重定向,但不会更新您的视图。 对于 ajax,您需要在成功时更新的部分视图。 因此,您只能使用按钮提交表单,而不是 ajax。 在这种情况下,一切都会正常工作。

您可以使用 ajax 的另一种方法是您可以尝试在属性中拆分订单并创建查询字符串。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM