0

My current situation is as follows. I have a form that when it gets submitted, passes the model into the controller and it does what it needs to do. At the end it redirects to a confirmation page that also gets passed the same model. All of that works fine, except when I am on the confirmation page, whenever I reload the page it resubmits the form.

I've tried using TempData but when I use that it requires for my model to be serializable and my model uses other models inside it that were made by other people that all need to be serializable which would result in like 15-20 different classes all needing to become serializable which just doesnt seem reasonable.

Here is some of what I am working with:

[HttpPost]
public async Task<ActionResult> SubmitClaim(WarrantyClaim model)
{ 
       ... code ...
      return BeddingWarrantyConfirmation(model);
}

public ActionResult BeddingWarrantyConfirmation(WarrantyClaim model)
{
    return View("BeddingWarrantyConfirmation",model);
}
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
  • 2
    You should research the "Post-Redirect-Get" pattern. – mason Jun 12 '18 at 18:06
  • If you are writing code like `return View("etc")` you are *not* redirecting. That terms means something [very specific](https://en.wikipedia.org/wiki/URL_redirection#Example_HTTP_response_for_a_301_redirect). If you were redirecting you wouldn't have this problem. – John Wu Jun 12 '18 at 18:09
  • 1
    Use the [PRG Patten](https://stackoverflow.com/questions/11209191/how-do-i-include-a-model-with-a-redirecttoaction/11209320#11209320) – Shyju Jun 12 '18 at 18:42

2 Answers2

3

You could make use of the Post Redirect Get Pattern. You could return the following in SubmitClaim:

return RedirectToAction("BeddingWarrantyConfirmation", "CONTROLLER", model);

For more information, please see https://en.wikipedia.org/wiki/Post/Redirect/Get

Thomas Gassmann
  • 737
  • 1
  • 13
  • 27
  • So I attempted doing this way, and once it actually gets to the "BeddingWarrantyConfirmation" action, parts of my model become null that werent previously null. – Brendan Bishop Jun 12 '18 at 18:23
  • Well, could you please explain further, which parts of your model are null? Additionally you have to remember that `RedirectToAction` will execute a GET request again on the client. Therefore the values should be sent in the query parameter. – Thomas Gassmann Jun 12 '18 at 18:25
0

This is a common problem in MVC development, you can follow the Post/Redirect/Get strategy to avoid the request.

https://en.wikipedia.org/wiki/Post/Redirect/Get