Vitaliy Vitaliy - 11 days ago 5
jQuery Question

block button on submit form

Trying to avoid multiple submission on submit button click using attribute

disabled
, but on click it just block the button and nothing happens. Have tried to add
id
to Html.Begin form, but on click it gives server application error like :
the parameters dictionary contains a null entry for parameter 'id' of non-nullable type System.Int32


@using (Html.BeginForm(MVC.Controller.MethodName(), FormMethod.Post, new { id = "submitFormId" }))


<button class="btn btn-w-m btn-primary" type="button" id="submitButtonId" value="AddSmth" onclick="return validateForm(event)">
<i class="fa fa-plus"></i>&nbsp;Add
</button>


And js:

$("#submitButtonId").attr("disabled",true);
$('#submitFormId').submit();

Answer

Founded another way to prevent multiple click on the button. Just added :

@{ Html.EnableClientValidation(); }
@{ Html.EnableUnobtrusiveJavaScript(); }

To the view, and after added global variable var isSubmited = false;, then validating it by:

if (isSubmited) e.PreventDefault();

. . .

if ($('#submitFormId').valid()) {
            isSubmited = true;
        }
Comments