2

I'm building an ASP.Net Mvc 4 app. I see that i can use a @Ajax.BeginForm to perform a POST request asynchronously. Also API seems to have an OnSuccess and On Failure parameter, which takes in the javascript function to be executed on success/failure. I've read that this works for MS Ajax, but would this work for jquery as well ? I can't seem to get it working with jquery alone

@using (Ajax.BeginForm("Create", "Comment", null, new AjaxOptions
                                                       {
                                                           InsertionMode = InsertionMode.Replace,
                                                           HttpMethod = "Post",
                                                           OnSuccess = "people.successfulSave",
                                                           OnFailure = "people.saveFailure"
                                                       },

If not then, i want to do it with jquery via form submit. Currently I'm doing

$('#commentSubmitInput').click(function() {
        $("#commentForm").submit();
    }

to submit the form. But in my controller I'm doing some model validation and return back Html with model errors if the model is invalid, else return a partial view with updated html. Now i read that i could bind the ajax call to a function with the below syntax

$("#commentForm").bind('ajax:complete', function () {
        alert('bla');
    });

but, is there a way I can have success and failure callback for form submit instead ?? kind of like the Jquery ajax call syntax

$.ajax({
            url: $(this).attr('ajaxUrl'),
            type: 'POST',
            success: function (data) {                   
                $("#comment_" + data.Id).remove();
            },
            error: function (data) {
                window.location.reload();
            }
        });

Any help/suggestion is appreciated.

Thanks in advance !

noob Mama
  • 267
  • 8
  • 28

1 Answers1

1

As far as I can help you is to check this: ASP.NET MVC 4 - Ajax.BeginForm and html5

And maybe this (you can get an idea or two here): MVC4 - Ajax.BeginForm and Partial view gives 'undefined'

Community
  • 1
  • 1
Peter Stegnar
  • 12,615
  • 12
  • 62
  • 80