Currently my form is being submitted through ASP.NET as well as through AJAX, but I only want it submitted through AJAX (and i don’t want to use Ajax.BeginForm because it’s smelly).
Please note I stripped data from the form down to just the elements involved, more or less.
@using (Html.BeginForm("Action", "Controller", FormMethod.Post))
{
<div style="width:275px;">
<a href="#" id="edit_licenses" class="grayBtn left">Edit</a>
<a href="#" id="cancel_licenses" style="display:none;" class="grayBtn left">Cancel</a>
<input id="submit_licenses" type="submit" style="display:none;" value="Save" class="redBtn right" />
</div>
}
<br />
</div>
<script type="text/javascript">
$('#edit_licenses').click(function () {
swap_licenses(false);
return false;
});
$('#cancel_licenses').click(function () {
swap_licenses(true);
$('form').resetForm();
return false;
});
$('form').submit(function (e) {
if ($(this).valid()) {
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
success: function (result) {
}
});
}
return false;
});
var swap_licenses = function (bool) {
$('.licenses').each(function () { $(this).attr('disabled', bool); });
$('#submit_licenses, #edit_licenses, #cancel_licenses').toggle();
}
</script>
Did you try using the
preventDefaultmethod to prevent the normal form submit ?Now you do not need to the event handler for form submit event as we already bounded that to the submit button.
Also form submitting/posting should be always a HTTP Post method. So you can simplify your
PostFormmethod like this