Reputation: 1105
I'm using asp .net mvc 4 and I have a html form using ajax validation.
I use this javascript code to know if the form is not valid. This code works.
$('#form').bind('invalid-form.validate', function () {
alert("error");
return false;
});
But I cannot find a javascript callback for success ?
I tried :
$('#form').bind('valid-form.validate', function () {
alert("success");
});
or
$('#form').validate({
invalidHandler:
function (form, validator) {
alert('error');
},
rules: function (form, validator) {
alert('success ');
}
});
But I never get the success alert. Thanks for your help.
Upvotes: 2
Views: 1421
Reputation: 3505
Bind to form.submit instead like this -
$('#form').on('submit', function (e) {
e.preventDefault();
if ($(this).valid()) { // This checks if the form is valid
alert("success"); // Or instead of success you can hide the form here
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
success: function (result) {
// Goes in here after your MVC controller action is done
},
error: function (result) {
// Goes in here if your mvc controller action returns an error
}
});
};
});
Upvotes: 0
Reputation: 1038
use ajax.beginform
@using (Ajax.BeginForm("Delete",new{id = Model.id}, new AjaxOptions
{
OnSuccess = "OnSuccess",
OnFailure = "OnFailure",
HttpMethod="Post"
}))
then change OnSuccess/Onfailure to name of your java script function
Upvotes: 1