开发者

MVC submit form to different controllers

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开发者_如何学C one to another controller/url (eg /action/anothervew).


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();});


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜