1

The controller action is called after the completedCallback not after $(this).submit(). See the code below. How can i trigger the action before raising the completed callback without using ajax?

controller:


public ActionResult MyFunction(FormCollection data){
//this should trigger before calling the CompletedEvent
}

javascript



var flag = false;

$(form).submit(function(){

        if(!flag){

        //raise start event
        startedCallback.call();

        flag = true;
        $(this).submit();

       //raise completed event
        completedCallback.call();

}

});


tereško
  • 58,060
  • 25
  • 98
  • 150
h3n
  • 5,142
  • 9
  • 46
  • 76
  • What do you mean without ajax? The example you give is by definition Asynchronous. If you submit a 'synchronous' form it means the script will exit after the $(this).submit() clause and follow up to the form page. Perhaps you don't want to use xml? Or are you using some kind of Iframe construction? Perhaps you can share an example of what you wish to accomplish? – Arend Jun 02 '11 at 09:25
  • You do know that Arend and Adam gave you the only "correct" answers you're going to get, right? – jcolebrand Jun 05 '11 at 22:33

2 Answers2

9

If you do not want to use ajax, you cannot reliably call something after the submit - the form's processing is done. You must use ajax if you want a response.

If you want to call your function BEFORE your form submit - again.. you MUST use some form of ajax.

See How do I capture response of form.submit

If you really need control of events like this - you need ajax or iframe hacks. Sorry : )

Community
  • 1
  • 1
Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71
0

Maybe your action can return a url var, like ?status=submitted?

Then check in your javascript if that var exists and if so, trigger your completedCallback.

By the way, you should return false in your $(form).submit(function(){return false;}); if you dont want the form to be submitted if flag == true.

jukempff
  • 965
  • 6
  • 12
  • Welcome to StackOverflow ju k! When you're writing code inline (within your text) you can surround it with back ticks so that it doesn't appear in the same font as regular text. For example, if you click to edit your answer, you'll see where I've added them for you. :) – Richard Marskell - Drackir Jun 06 '11 at 18:52