views:

442

answers:

1

Submitting an Ajax.Beginform while using the JQuery Validate plugin doesn't stop the submission. Is there a good way to ensure the Ajax.Beginform is not submitted?

I've also tried using the JQuery Forms plugin to 'ajaxify' my form. This works a treat. However, my form is a login form and if the user successfully logs in, I'd like them to be redirected. I've done this in Ajax.BeginForm by returning a JavaScript object, but the same code using JQuery.Form.js simply displays the Javascript rather than executing it. Is there a way to manage this?

[as requested below, I've added some sample code]

Controller Code

            if (Request.IsAjaxRequest())
            {
                return Content("/receipt/latest");
            }
            else
            {
                return RedirectToAction("latest", "receipt");
            }

I've used this to return a string to this js function:-

JS

            function manageResponse(responseText, statusText) {
                if (responseText.toString().substr(0, 1) == '/') {
                    window.location = responseText;
                }
                else {
                    $("#formResult").text(responseText);
                }
            }

I've used these options to tie the response with JQuery Form plugin.

            var options = {
                beforeSubmit: function() {
                    return $('#login').validate().form();
                },
                success: manageResponse
            };

It feels a bit 'hacky' to me, but it works. I was enquiring whether there was a better way or not?

Rob

+1  A: 

It doesn't seem too hacky to me, but them I'm not the most elegant guy in the world. The only thing I think I'd do differently is set it up like this:

$('#login').validate(); // setup form to use validation plugin 

var options = {
    beforeSubmit: function() {
        return $('#login').valid(); // check form is valid
    },
    success: manageResponse
};
jammus