I have a simple form created using Ajax.BeginForm
:
<% using (Ajax.BeginForm(\"Update\", \"Description\", new { id = Model.Id
Simply place normal button indide Ajax.BeginForm and on click find parent form and normal submit. Ajax form in Razor:
@using (Ajax.BeginForm("AjaxPost", "Home", ajaxOptions))
{
<div class="form-group">
<div class="col-md-12">
<button class="btn btn-primary" role="button" type="button" onclick="submitParentForm($(this))">Submit parent from Jquery</button>
</div>
</div>
}
and Javascript:
function submitParentForm(sender) {
var $formToSubmit = $(sender).closest('form');
$formToSubmit.submit();
}
Unfortunately triggering the onsubmit or submit events wont work in all browsers.
Also, if you trigger('submit') in Chrome or IE, it causes the entire page to be posted rather than doing an AJAX behavior.
What works for all browsers is removing the onsubmit event behavior and just calling submit() on the form itself.
<script type="text/javascript">
$(function() {
$('form#ajaxForm').submit(function(event) {
eval($(this).attr('onsubmit')); return false;
});
$('form#ajaxForm').find('a.submit-link').click( function() {
$'form#ajaxForm').submit();
});
}
</script>
<% using (Ajax.BeginForm("Update", "Description", new { id = Model.Id },
new AjaxOptions
{
UpdateTargetId = "DescriptionDiv",
HttpMethod = "post"
}, new { id = "ajaxForm" } )) {%>
Description:
<%= Html.TextBox("Description", Model.Description) %><br />
<a href="#" class="submit-link">Save</a>
<% } %>
Also, the link doesn't have to be contained within the form in order for this to work.