In this article I will explain with an example, how to prevent / avoid / stop Form Resubmission (Resubmit) when Refresh button clicked in Browser in ASP.Net MVC Razor.
The problem is that when browser refresh button or F5 or CTRL + F5 function keys are pressed, the last executed event is again executed.
For example, if you have a Form submission on Button click and after clicking button, page is refreshed by the user then the Form is resubmitted and again same Action method gets called.
Note: For beginners in ASP.Net MVC, please refer my article ASP.Net MVC Hello World Tutorial with Sample Program example.
 
 
Understanding the problem
The problem is that when browser refresh button or F5 or CTRL + F5 function keys are pressed, the last executed event is again executed.
For example, if you have a Form submission on Button click and after clicking button, page is refreshed by the user then the Form is resubmitted and again same Action method gets called.
Let us understand with a simple example using an 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.
Note: For details about Form Post in MVC, please refer my article ASP.Net MVC: Form Submit (Post) example.
 
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.
ActionName – Name of the Action. In this case the name is Index.
ControllerName – Name of the Controller. In this case the name is Home.
FormMethod – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
The Form consists of a Submit Button and when the Button is clicked, the Form is submitted.
@{
    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>
 
Execution
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.
ASP.Net MVC: Prevent (Avoid) Form Resubmission (Resubmit) when Refresh clicked in Browser
 
 

Solution
The solution is very simple, either the page must be redirected to itself or to some other page in order to avoid this particular behavior.
In the below example, the redirection is done to the same Action method which ultimately solves this issue.
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.
Finally, a redirection is performed to the Index Action method.
public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        return View();
    }
 
    [HttpPost]
    public ActionResult Index(string name)
    {
        //Send to the Index Action method which will prevent resubmission.
        return RedirectToAction("Index");   
    }
}
 
View
The View is exactly same and there is no change.
@{
    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>
 
Execution
After Form submission, now when the Refresh Button in the Browser is clicked or the F5 key is pressed, nothing happens.
ASP.Net MVC: Prevent (Avoid) Form Resubmission (Resubmit) when Refresh clicked in Browser
 
 
Downloads