Search code examples
.netasp.net-mvcasp.net-coreasp.net-web-api.net-5

Confirm form resubmission - ASP.NET 5 MVC


I have two controllers Booking and Checkout. Posting form data from controller Booking to Checkout index get page.

I want to retrieve the object submitted when clicking the back button from Checkout to Booking index get page.

(same error applies even if I refresh the page meanwhile I am in checkout page)

Confirm Form Resubmission
This webpage requires data that you entered earlier in order to be properly displayed. 
You can send this data again, but by doing so you will repeat any action this page previously performed.
Press the reload button to resubmit the data needed to load the page.

ERR_CACHE_MISS

BookingController

public async Task<IActionResult> Index(CarsBookingVM carsBooking)
{
    return View(carsBooking);
}

BookingIndex.cshtml

<form asp-controller="Checkout" asp-action="Index">
<input hidden value="@Model.CarId" name="carId">
<input type="submit" value="Proceed">
</form>

CheckoutController

[HttpGet]
public async Task<IActionResult> Index(int? carId)
{
    BookingVM booking = new BookingVM(){
    //...

    return View(booking);
}

[HttpPost]
public async Task<IActionResult> Create(BookingVM booking)
{
    //...
}

Solution

  • Easy peazy , Just set method="get" in form tag like this :

    <form asp-controller="Checkout" asp-action="Index" method="get"  >
        <input hidden value="@Model.CarId" name="carId">
        <input type="submit" value="Proceed">
    </form>
    

    The browsers care about 'post' but not 'get' .