Ajax message and redirect using MVC4 model value

I am posting the page via agax post with json data and then redirecting to another view. It works great.

 $.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 controller

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

      

But now I wanted to pass the bookingInfo object to booking mode. I know I can go through the query string, but is it possible to bind this kind of model object reservation?

+1


source to share


1 answer


Instead of window.location.href

on successful callback

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

      

You can make another call ajax

/ $.post

here and pass your object using the method 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
            });
        }
    });

      

Update: Maybe TempData dictioanry might be helpful here ...

[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
}

      

0


source







All Articles