In an ASP.NET MVC application, 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.
Let us understand with a simple example using an ASP.NET MVC project.
Controller
The Controller consists of following Action methods.
Action method for handling GET operation: Inside this Action method, simply the View is returned.
Action method for handling POST operation: This Action method handles the Form Submission when the Button is clicked.
When the Submit Button is clicked, the value of the Name TextBox is received as parameter.
Finally, the View is returned.
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(string name)
{
return View();
}
}
View
The View consists of an HTML Form which has been created using the Html.BeginForm method with the following parameters.
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width"/>
<title>Index</title>
</head>
<body>
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<input type="text" name="Name"/>
<input type="submit" value="Submit"/>
}
</body>
</html>
After Form submission, when the Refresh Button in the Browser is clicked or the F5 key is pressed, a warning popup comes up which warns against Form resubmission.