Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC How to Prevent re-POST

Using MVC 4.

When an order is placed on our site the order is POSTed to:

    [HttpPost]
    public ActionResult ConfirmOrder(ABCModel model)
    {
        //Do Stuff
        return View("ConfirmedOrder", model);
    }

The user sees the Confirmed page.

If they press REFRESH in their browser, the page POSTs again.

Is there a way in MVC to prevent the POST again, perhaps in a redirect or some sort?

like image 973
Ian Vink Avatar asked Oct 23 '13 20:10

Ian Vink


2 Answers

Instead of doing

return View("ConfirmedOrder", model)

separate your confirmation logic into a controller and do

return RedirectToAction("ConfirmOrderActionName").

Here your ConfirmOrderActionName controller can retrieve the order information from the data store and sent it to its own view, or your ConfirmedOrder view.

P.S.

Note that RedirectToAction() helper method also returns a type of ActionResult (just like returning a View() does).

If you're interested see:

MSDN: Controllers and Action Methods in ASP.NET MVC Applications and MSDN: ActionResult Class

like image 177
nomad Avatar answered Sep 27 '22 20:09

nomad


You might want to redesign the logic a little bit. It is a command problem in Shopping Cart check out.

Here is how most Shopping Cart works -

Step 1. Cart (Create a Session here)

... Shipping, Payment and so on

Step 2: ConfirmOrder - Get (If no Session, redirect to Cart page.)
        ConfirmOrder - Post (If no Session, redirect to Cart page. If valid and 
                             check out successful, redirect to Complete page)

Step 3: Complete (Clear the Session)
like image 38
Win Avatar answered Sep 27 '22 21:09

Win