MVC presents a form for different controllers

Is it possible to place the same form for different controllers?

Each page can only be sent to form the action URL, but it can be somehow, how can I tell the button on which the url form should be displayed?

For example, I have one form and two submit buttons, one button will send the form to one controller / URL (e.g. / action / view), and send the form to another controller / URL (e.g. / action / anothervew) .

+6
asp.net-mvc-2
source share
3 answers

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

JQuery will be something like:

$('#button1').onclick(function(){ $(this).action = url1; $(document).submit();}); $('#button2').onclick(function(){ $(this).action = url2; $(document).submit();}); 
+4
source share

You will need to use javascript for this. When the button is clicked, javascript will change the action property of the form to the appropriate controller and then submit the form.

+2
source share

We did this before using javascript, as mentioned in other answers, and probably the right way. An alternative, however, is to publish on a single controller method that contains logic to decide where to send form data to.

In fact, you submit the form to the controller and resubmit this data based on the text or button identifier pressed with the if statement in the body of the controller action.

+1
source share

All Articles