【问题标题】:Trouble sending JSON data to another action in asp.net MVC controller无法将 JSON 数据发送到 asp.net MVC 控制器中的另一个操作
【发布时间】: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


    【解决方案1】:

    您的模型与 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对象,其中有两个签名属性):

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

    【讨论】:

    • 我尝试了这两种解决方案,但都不适合我。
    【解决方案2】:

    你不能用

     window.location.href = ...
    

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

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

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

    或者当它是同一个控制器时,我通常会这样做

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

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

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

    【讨论】:

      猜你喜欢
      • 2012-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-30
      • 2016-03-28
      • 2014-03-07
      • 2016-07-22
      相关资源
      最近更新 更多