How to post ASP.NET MVC Ajax form using JavaScript rather than submit button How to post ASP.NET MVC Ajax form using JavaScript rather than submit button jquery jquery

How to post ASP.NET MVC Ajax form using JavaScript rather than submit button


I'm going to assume that your lack of quotes around the selector is just a transcription error, but you should check it anyway. Also, I don't see where you are actually giving the form an id. Usually you do this with the htmlAttributes parameter. I don't see you using the signature that has it. Again, though, if the form is submitting at all, this could be a transcription error.

If the selector and the id aren't the problem I'm suspicious that it might be because the click handler is added via markup when you use the Ajax BeginForm extension. You might try using $('form').trigger('submit') or in the worst case, have the click handler on the anchor create a hidden submit button in the form and click it. Or even create your own ajax submission using pure jQuery (which is probably what I would do).

Lastly, you should realize that by replacing the submit button, you're going to totally break this for people who don't have javascript enabled. The way around this is to also have a button hidden using a noscript tag and handle both AJAX and non-AJAX posts on the server.

BTW, it's consider standard practice, Microsoft not withstanding, to add the handlers via javascript not via markup. This keeps your javascript organized in one place so you can more easily see what's going on on the form. Here's an example of how I would use the trigger mechanism.

  $(function() {      $('form#ajaxForm').find('a.submit-link').click( function() {           $('form#ajaxForm').trigger('submit');      }).show();  }<% 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" style="display: none;">Save</a>   <noscript>       <input type="submit" value="Save" />   </noscript><% } %>


A simple example, where a change on a dropdown list triggers an ajax form-submit to reload a datagrid:

<div id="pnlSearch">    <% using (Ajax.BeginForm("UserSearch", "Home", new AjaxOptions { UpdateTargetId = "pnlSearchResults" }, new { id="UserSearchForm" }))    { %>        UserType: <%: Html.DropDownList("FilterUserType", Model.UserTypes, "--", new { onchange = "$('#UserSearchForm').trigger('submit');" })%>    <% } %></div>

The trigger('onsubmit') is the key thing: it calls the onsubmit function that MVC has grafted onto the form.

NB. The UserSearchResults controller returns a PartialView that renders a table using the supplied Model

<div id="pnlSearchResults">    <% Html.RenderPartial("UserSearchResults", Model); %></div>


Unfortunately triggering the onsubmit or submit events wont work in all browsers.

  • Works in IE and Chrome: #('form#ajaxForm')trigger('onsubmit');
  • Works in Firefox and Safari: #('form#ajaxForm')trigger('submit');

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.