Frnds i want to submit data to two different controllers using two different buttons on one form and i want that both buttons call different actions and also provide all the values of the form to their respected controller.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You can do this with JavaScript like so:
HTML
<form method="POST" action="/">
... The Rest Of Your Form ...
<input type="Submit" id="btn-1" name="btn-1" />
<input type="Submit" id="btn-2" name="btn-2" />
</form>
JavaScript
(function(d) {
var form = d.getElementsByTagName('form')[0],
targetBtn;
form.onsubmit = function(e) {
e.preventDefault();
targetBtn = e.explicitOriginalTarget.id;
//Set the form action based on the id of the button that submitted it
if(targetBtn === 'btn-1') {
form.action = '/foo';
} else {
form.action = '/bar'
}
//Submit your form
form.submit();
};
}(document));
All you need to do is make sure this code is placed after the from in the HTML markup or add it to a jQuery document.ready function.
回答2:
here is a good article that can help you accomplish what you want
http://blog.ashmind.com/2010/03/15/multiple-submit-buttons-with-asp-net-mvc-final-solution/