In this article I will explain with an example, how to prevent double click of Submit button in ASP.Net MVC.
Note: For beginners in ASP.Net MVC, please refer my article ASP.Net MVC Hello World Tutorial with Sample Program example.
 
 
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 gets called, when the Submit Button is clicked.
Inside the Action Method, the Thread.Sleep function is used to delay the process.
Note: Thread.Sleep function is used to simulate a long running process. Thus, it must be removed when programming in actual project.
 
Finally, a message is set into a ViewBag object.
public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        return View();
    }
 
    [HttpPost]
    public ActionResult Index(string name)
    {
        //Added to simulate long running process.
        //Remove before publishing.
        System.Threading.Thread.Sleep(5000);
 
        ViewBag.Message = "Form submitted.";
        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 HTML Form consists of a HTML TextBox and a Submit Button.
When the Submit button is clicked the Form is submitted and the message is displayed using ViewBag object.
 
Disabling Submit button after click
For disable Submit button after Click using JavaScript, please refer my article Disable Submit button after click using JavaScript and jQuery.
@{
    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))
    {
        <table cellpadding="0" cellspacing="0">
            <tr>
                <th colspan="2" align="center">Details</th>
            </tr>
            <tr>
                <td>Name: </td>
                <td><input type="text" name="name" value="" /></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="Submit" /></td>
            </tr>
        </table>
        <b style="color:green">@ViewBag.Message</b>
    }
 
    <script type="text/javascript">
        window.onbeforeunload = function () {
            var inputs = document.getElementsByTagName("INPUT");
            for (var i = 0; i < inputs.length; i++) {
                if (inputs[i].type == "button" || inputs[i].type == "submit") {
                    inputs[i].disabled = true;
                }
            }
        };
    </script>
</body>
</html>
 
 
Screenshot
Prevent double click of Submit Button in ASP.Net MVC
 
 
Demo
 
 
Downloads