MS MVC form AJAXifying techniques [closed]

Here is my solution (I think it is a Progressive enhancement solution), using only jQuery without any plugins:

var form = $('form#YourFormId');
$(':submit', form).click(function (event) {
    event.preventDefault();
    $.post(form.attr('action'), form.serialize(),
        function(data, status) {
            if(status == 'success') {
                // your code here
            }
        }
    );
});

UPDATED:

If your POST response is “HTML with form” then try this:

function ajaxifyForm(form) {
    $(':submit', form).click(function (event) {
        event.preventDefault();
        $.post(form.attr('action'), form.serialize(),
            function(data, status) {
                if(status == 'success') {
                    var newForm = $(data);
                    ajaxifyForm(newForm);
                    form.after(newForm).remove();
                }
            }
        );
    });
}

Leave a Comment