Disable Button on Click in ASP.Net MVC

Last Reply 7 months ago By dharmendr

Posted 7 months ago

hello,

i want when user click on submit button, button should disable and text should show as Progressing.....

You are viewing reply posted by: dharmendr 7 months ago.
Posted 7 months ago

Hi nauna,

Check this example. Now please take its reference and correct your code.

Controller

public class HomeController : Controller
{
    // GET: /Home/
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Save()
    {
        // ... do something task
        ViewData["IsEnabled"] = false;
        return View("Index");
    }
}

View

<html>
<head>
    <title>Index</title>
    <script type="text/javascript" src="https://code.jquery.com/jquery-2.2.3.js"></script>
    <script type="text/javascript">
        $(function () {
            var enabled = '<%= ViewData["IsEnabled"] %>'
            if (enabled == 'False') {
                $(this).removeAttr("disabled");
                $(this).val("Save");
            }
            $('#btnSave').on('click', function () {
                $(this).attr('disabled', 'disabled');
                $(this).val("Progressing.....");
            });
        });
    </script>
</head>
<body>
    <div>
        <%using (Html.BeginForm("Save", "Home", FormMethod.Post))
          {%>
        <input type="submit" id="btnSave" value="Save" />
        <% } %>
    </div>
</body>
</html>

Screenshot