简体   繁体   中英

ASP.NET MVC HttpPost posting null model

Form posts from webpage MakeBooking to FinalBooking to ascertain certain information such as number of guests, so the FinalBooking page can give you enough textboxes to input guest information for all guests required.

When in debug mode, both models in MakeBooking post are populated. After post, in FinalBooking , model is null.

    [HttpPost]
    public ActionResult MakeBooking(BookingModel model)
    {
        return RedirectToAction("FinalBooking", "Booking", new { model = model });
    }

    public ActionResult FinalBooking(BookingModel model)
    {
        return View(model);
    }

Any info would be appreciated.

它应该工作

return RedirectToAction("FinalBooking", "Booking", model);

You can not pass a model with RedirectToAction like that. you need to use either TempData or Session to transfer the model object between your calls.

RedirectToAction method returns an HTTP 302 response to the browser, which causes the browser to make a GET request to the specified action.

The below example shows how to transfer data using TempData.

[HttpPost]
public ActionResult MakeBooking(BookingModel model)
{
    TempData["TempBookingModel"]=model;
    return RedirectToAction("FinalBooking", "Booking");
}

public ActionResult FinalBooking()
{       
    var model= TempData["TempBookingModel"] as BookingModel; 
    return View(model);
}

Internally TempData is using Session as the storage mechanism.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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