In this article I will explain with an example, how to prevent double click of Submit button in ASP.Net Core MVC.
Note: For beginners in ASP.Net Core 7, please refer my article ASP.Net Core 7: 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
{
    public IActionResult Index()
    {
        return View();
    }
 
    [HttpPost]
    public IActionResult Index(string name)
    {
        //Added to simulate long running process.
        //Remove before publishing.
        System.Threading.Thread.Sleep(5000);
 
        ViewBag.Message = "Form submitted.";
        return View();
    }
}
 
 
View
Inside the View, first the ASP.Net TagHelpers is inherited.
The View consists of an HTML Form which has been created using the ASP.Net TagHelpers with the following attributes.
asp-action – Name of the Action. In this case the name is Index.
asp-controller – Name of the Controller. In this case the name is Home.
method – 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 disabling Submit button after Click using JavaScript, please refer my article Disable Submit button after click using JavaScript and jQuery.
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <form method="post" asp-controller="Home" asp-action="Index">
        <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>
    </form>
 
    <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 Core
 
 
Demo
 
 
Downloads