views:

45

answers:

3

Is it possible to post the same form to different controllers?

Each page could be post only to form action url, but may be some how i may say to button to which url form should go?

e.g i have one form and two submit buttons, one button will post form to one controller/url (eg /action/view) anther button submit form to one to another controller/url (eg /action/anothervew).

+3  A: 

You can definitely do this, use JQuery (or just javascript) to attach a function to the onclick event of the button(s). Then use that function to change the URL that the form posts to and then submit the form.

JQuery would be something like:

$('#button1').onclick(function(){ $(this).action = url1; $(document).submit();});
$('#button2').onclick(function(){ $(this).action = url2; $(document).submit();});
Paul Hadfield
+1  A: 

You will need to use javascript for this. When the button is clicked have the javascript modify the form's action property to the appropriate controller and then submit the form.

David
+1  A: 

We've done this before using javascript, as mentioned in other answers, and that's probably the correct way to go. An alternative, however, is to post to a single controller method which contains logic to decide where to send the form data off to.

Effectively, you submit the form to the controller, and the resubmit that data based on the text or id of the button clicked using an if statement in the body of the controller action.

JonNeale