I use MVC and would like to submit a form via jquery, but for the mvc built in validation to work in case of a any invalid input, I’ve conditioned the “return false” of the click event (also tried “event.preventDefault”), in case of getting a “success” json return from the controller.
Here’s the code:
$(document).ready(function () {
var request;
$('#formSubmit').click(function (event) {
request = $.ajax({
url: '/personManager/loadDetails/',
type: 'post',
data: $('form').serialize()
});
request.done(function(data){
if(data=="success")
event.preventDefault();
});
});
});
I can’t make it work…
What do I do wrong?
also, is there any more efficient way to preserve mvc validation mechanism, while working with jquery?
updated: If I try to describe my wish more simply: I want to condition the preventDefault of the click event, so in a certain condition it will invoke the form submit, and in other, it won’t, and only $.ajax work.
The second accures after I confirm on the server side that no action has taken place.
There’s must be a way, I guess…
The problem is most likely because your ajax is asynchronous. Set it to be synchronous and your code should work fine. However be aware that if the server fails to respond instantaneously you risk your user’s browser freezing up.