2

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.

2
  • Are you doing an Ajax form post, or regular? Do you want the popup to appear before or after returning from the server? Commented Jul 25, 2013 at 1:45
  • actually I don't want to use the alert function now it's just for debugging, I want to hide a div by doing something like this $("myid").hide(). But I need to know if it's success or not Commented Jul 25, 2013 at 1:52

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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                   
                }
            });
        };
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.