簡體   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