0

In a conventional ASP MVC POST action the user-modified model is returned to the view directly, as described here in Darin's answer:

    if (!ModelState.IsValid)
    {
        return View(model);
    }

However, using the strict Post-Redirect-Get pattern, described on SO here and and by Kazi Manzur Rashid here (#13), the ModelState is preserved, but the model is recreated afresh on the original GET action. So how does the user get her entered values back?

    if (!ModelState.IsValid)
    {
        return RedirectToAction("index");
    }

I see that my browser I am using restores the values, but that is relying on the browser. Is this standard browser behaviour I can rely on? Have I missed something obvious?

2 Answers 2

0

I don't think you understand Post-Redirect-Get. You post back to the same view that rendered the form in the first place. If there's an error, you just return the view back. Only if the submission is valid and successful do you redirect to a new view.

To illustrate, all your Post-Redirect-Get style workflows will include actions in the form of:

// Original GET that returns form to user
public ActionResult SomeForm()
{
    return View();
}

// Receives posted data and returns same view, based on having the
// same action name
[HttpPost]
public ActionResult SomeForm(PostDataModel model)
{
    if (ModelState.IsValid)
    {
        // save to database or whatever
        // Success, so you redirect
        return RedirectToAction("SomeFormSuccess");
    }

    // Errors, so you return view
    return View(model);
}

// Totally separate action that serves as place to drop the user so
// refreshing the page does not resubmit the form. If you want to
// redisplay the data the user posted, you query it back from the
// database or wherever else it was stored.
public ActionResult SomeFormSuccess()
{
    return View();
}
3
  • That's the way I have always done it, but I am investigating another pattern - the so-called strict-PRG pattern as referred to on stackoverflow.com/a/5538843/1943145 where you do a redirect even when the validation fails.
    – GuyB
    Jun 30, 2014 at 15:06
  • That's the dumbest thing I've ever heard of. Regardless, the only reason to do something like that would be if you actually wanted to dump the posted data. If you want to do "strict-PRG", but keep the posted data, you're talking about vanilla PRG. Anything else is a complete bastardization of the whole HTTP protocol. Jun 30, 2014 at 15:11
  • I think I'll avoid this pattern in the absence of wider acceptance. Thanks for your view!
    – GuyB
    Jul 1, 2014 at 9:10
-1

This sounds like a good use for MVC's TempData property bag which lets you store data in one request and access it in the very next request.

1
  • This is the mechanism used in the described pattern for passing the ModelState, so it seems reasonable to pass the model this way as well. Irrespective of whether the pattern is valid, this seems a valid answer, so why the mark-down?
    – GuyB
    Jul 1, 2014 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.