Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a simple form created using Ajax.BeginForm:

<% 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 /> <input type="submit" value="save" /> <% }%> 

The controller is wired up and returns a partial view that updates the DescriptionDiv. And it all works neatly.

Now I would like to be able to submit this form without having the submit button (via a clik on a link or on an image or whatever). Unfortunately this little jQuery snippet does not do the job:

$('form#AjaxForm').submit(); 

It does submit the form, but does (I suppose not surprisingly) a regular post-back and not an Ajax one.

For the sake of simplicity the above jQuery is wired up like this:

<a href="#" onclick="$('form#AjaxForm').submit(); return false;">submit</a> 

The form's onsubmit is using the Sys.Mvc.AsyncForm.handleSubmit() but the jQuery submit seems to be bypassing this.

PS. I am looking for a solution in this particular approach. I know how to achieve the same using a normal form and posting it using AJAX+jQuery. I am interested in this particular solution though.

like image 973
mfloryan Avatar asked Aug 20 '09 11:08

mfloryan


People also ask

Which JavaScript library is used by ASP NET MVC to make AJAX calls?

Another major advance to JavaScript and Ajax is the JavaScript object library called jQuery, which is the free, open-source software. It is a wrapper for JavaScript. jQuery is used to write the JavaScript to navigate and manipulate a page and make asynchronous Ajax callbacks.

How can make AJAX call in ASP NET MVC?

Open your Visual Studio and create a empty ASP.NET MVC application. Click on File -> New Project -> Web -> ASP.NET web application. From the next window Select template Empty and from Add folders and core reference choose MVC. Name it as AJAXCalls and click Ok.


1 Answers

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> <% } %> 
like image 198
tvanfosson Avatar answered Sep 28 '22 21:09

tvanfosson