MVC在Html.Begin形式需要两个按钮调用与表单的所有值不同的动作 [英] MVC In Html.Begin form need two buttons call different actions with all the values of the form
本文介绍了MVC在Html.Begin形式需要两个按钮调用与表单的所有值不同的动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
FRNDS我想用两个不同的按钮提交数据到两个不同的控制器一种形式的,我想这两个按钮调用不同的操作,并提供表格的所有值设为其推崇的控制器。
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.
推荐答案
您可以用JavaScript像这样做:
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));
所有你需要做的就是确保这code在HTML标记后,放置在距离或将其添加到一个jQuery的document.ready功能。
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.
这篇关于MVC在Html.Begin形式需要两个按钮调用与表单的所有值不同的动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文