Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
38
rated 0 times [  39] [ 1]  / answers: 1 / hits: 102734  / 15 Years ago, thu, august 20, 2009, 12:00:00

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.


More From » asp.net-mvc

 Answers
25

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>
<% } %>

[#98868] Monday, August 17, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
elijahm

Total Points: 674
Total Questions: 124
Total Answers: 79

Location: Northern Mariana Islands
Member since Fri, Jan 15, 2021
3 Years ago
;