1

我正在通过带有 json 数据的 agax 帖子提交一个页面,然后重定向到其他视图。它工作正常。

 $.ajax({
            url: '/bus/result',
            type: "POST",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            data: ko.toJSON(bookingInfo),
            success: function (data, textStatus, xhr) {
                window.location.href = data.redirectToUrl;
            }
        });

MVC 控制器

[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Result(BusBookingInfo bookingInfo)
        {
            if (Request.IsAjaxRequest())
            {
                return Json(new { redirectToUrl = Url.Action("Booking") });
            }

            //return Redirect("/bus/booking/");
            return RedirectToAction("result");
        }

但现在我想将 bookingInfo 对象传递给 Booking 视图。我知道我可以通过查询字符串但可以绑定这个模型对象预订视图?

4

1 回答 1

3

而不是window.location.href在成功回调中,

success: function (data, textStatus, xhr) {
    window.location.href = data.redirectToUrl;
}

您可以在此处进行另一个ajax/调用,并使用方法$.post传递您的对象。POST

$.ajax({
        url: '/bus/result',
        type: "POST",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: ko.toJSON(bookingInfo),
        success: function (data, textStatus, xhr) {
            $.post(data.redirectToUrl, bookingInfo, function(){
               //TODO: callback
            });
        }
    });

更新: 也许 TempData 字典在这里会有所帮助...

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Result(BusBookingInfo bookingInfo)
{
   if (Request.IsAjaxRequest())
   {
     TempData["ViewModelItem"] = bookingInfo;
     return RedirectToAction("Booking");
   }
   //return Redirect("/bus/booking/");
   return RedirectToAction("result");
}        

public ActionResult Booking()
{
   var bookingInfo = (BusBookingInfo)TempData["ViewModelItem"];
   //TODO: code
}
于 2013-06-26T08:32:17.027 回答