What is the Post/Redirect/Get pattern, and why use it?
Quick Answer
After a successful form POST, instead of returning a view directly, you RedirectToAction to a GET action. This means the browser's address bar shows a GET URL, so pressing refresh re-issues the GET — not the POST — preventing accidental double submissions and the “confirm form resubmission” dialog.
Post/Redirect/Get means after a form submission succeeds, I don't render a view from the POST action — I redirect to a GET action, usually the details page for the record I just created. The reason is the browser refresh: if the page you're looking at came from a POST, refreshing re-submits the form and you get a duplicate, or that annoying resubmission warning. After the redirect the URL is a GET, so refresh just re-fetches the page. If validation fails, though, I do return the view directly so the user sees their input and the errors.
- POST fails validation → return the view with
ModelStateerrors (no redirect — you need to show the errors and the entered data). - POST succeeds → save, put a flash message in
TempData,return RedirectToAction(nameof(Details), new { id }). - Also gives a bookmarkable, shareable result URL.