I'll take a shot at it based on how I understood your problem. So you have:
$(function () {
$("#submitbtn").click(function () {
$("#loading").fadeIn();
if (validFormInputs()) {
yourForm.submit();
}
else {
// hide the laoder again
$("#loading").fadeOut(); // or .hide or however you want to hide it
}
});
});
Now the loader may not show at all if the submit action happens too quickly, since the submit is a post back and not an ajax post. If on the server-side there is an error that was not caught by your client-side validation then you can just return the view again and you do not have to do something special on your javascript. So your method may look like this:
[HttpPost]
public ActionResult Edit(YourModel input) {
if (!ModelState.IsValid) {
return View(input);
}
// do something here, redirect or return the view again
}
Again, please take note that this code is for a post back action and NOT for an ajax post.
UPDATE: So basically here is the step:
- Capture the click event of your submit button
$("#submitbtn").click(function ()...
- Show your loader
$("#loading").fadeIn();
- Do the client side validation
validFormInputs()
If validation fails then hide your loader
$("#loading").fadeOut();
4a. Assumes you are using a validation plugin or in any case, you should have shown the error messages
If the validation succeeds then you submit the form
yourForm.submit();
If server-side validation fails then you return the view
return View(input);