views:

1067

answers:

2

Hi,

i'm trying to submit my Ajax form using jQuery. However calling the submit() function causes the entire page to refresh. It should just execute the onSubmit part of the form (which returns false so that the page shouldn't refresh).

<%Ajax.BeginForm("AllocateAndUpdateMech", 
  New With {.Controller = "Planning", 
            .Id = Model.Id}, 
            New AjaxOptions With {
                       .LoadingElementId = "loading", 
                       .UpdateTargetId = Model.Id & "_alloc"}, 
            New With {.id = "allocate_" & Model.Id & "_" & item.UserId}
)%>
<%Html.Hidden("mechId", item.UserId)%>
<a href="javascript:void(0);" onclick="$('#<%="allocate_" & Model.Id & "_" & item.UserId %>').submit();">Allocate: <%=item.UserName%></a>
<%Html.EndForm()%>
+1  A: 

When you call submit() on the form it does not call the onSubmit function that has been created by using that ajax form functionality. You will need to call the forms onSubmit directly

e.g $("#ViewCartPage form").onsubmit() or better just this.form.onsubmit()

Dont forget to include any parameters that that are needed by the Sys.Mvc.AsyncForm.handleSubmit event (not sure what they are as I don't use it).

I would really consider dropping the ms ajax js library and just sticking with jQuery. The ms js libs are full of badly written obtrusive muck that just make things over complicated for no reason. Things are much simpler using just jquery.

redsquare
the submit() function already returns false...?
Ropstah
returns it to what?
redsquare
<form onsubmit="Sys.Mvc.AsyncForm.handleSubmit(....);"> - the Sys.Mvc.... function returns false.
Ropstah
would help if we could see all of that
redsquare
but you need return bla.....to actually return the false!
redsquare
The <form> tag is generated based on <% Ajax.BeginForm() %>, I thought this was logical? It is just a simple Ajax form, if I use a submit -button- it works, because it somehow calls the form.onsubmit function. But if I 'manually' call it using jQuery, the onSubmit part seems to be ignored...?
Ropstah
but I cant see that it generates an obtrussive onsubmit function can I. Logical would be to get rid of all that inline script nonsense and follow protocol.
redsquare
It generates the -default- onsubmit. The one that handles a form submit using ASP.NET MVC Ajax. I don't want to be cynical, but do you want me to post my web.config, and my routes file also?
Ropstah
ropstah, no cheers, I cant imagine the mess that they are in if that is the state of a simple view
redsquare
+4  A: 

If you use the HTML Ajax.BeginForm you cant just commit the form, because the form is somehow hooked up with ASP.NET MVC Ajax.

There are 3 solutions:

Easiest and best way

Use jquery $.AJAX to commit the form

Easy but strange way

Put a submit button on the form. Set it to hidden and click it by jquery.

Difficult but the answer to your question

I have seen it someplace that you can still do exactly what you describe, but it was a workaround it didnt look nice.

Here is how its done submit form sample

Malcolm Frexner