有没有一种简单的方法可以实现这一目标?
不,但是您可以让控制器操作传递在结果中单击的按钮。这可以作为 Json 属性(如果您要返回 JSON)来完成,也可以是自定义响应 HTTP 标头。
然后在您的成功回调(只能是一个)中,您可以检索此值以了解单击了哪个按钮并采取相应措施。
因此,首先为您的提交按钮命名,以便您知道点击了哪个按钮:
@using (Ajax.BeginForm("Save", "Company", new AjaxOptions() {
HttpMethod="Post", OnSuccess="onSuccess"
}, new { id = "companyEditForm" })) {
....some edit fields......
<button type="submit" name="btn" value="save_next">Save & Next</button>
<button type="submit" name="btn" value="save">Save</button>
}
然后在你的控制器动作中
[HttpPost]
public ActionResult Save(MyViewModel model)
{
Response.AppendHeader("X-Button", Request["btn"]);
... your usual processing
}
最后在你的onSucecss
回调中:
function onSuccess(data, status, xhr) {
function onSuccess(data, status, xhr) {
var btn = xhr.getResponseHeader('X-Button');
if (btn == 'save_next') {
// The "Save & Next" button was clicked
} else if (btn == 'save') {
// The "Save" button was clicked
} else {
// The user didn't submit the form by using the mouse and
// clicking on a button, he simply pressed Enter while
// inside some text field or you have some other portion of
// javascript which triggered the form submission without pressing
// on any submit button
}
}
}