5

我的 MVC 页面上有一个 Ajax 表单,有两个单独的提交按钮......

@using (Ajax.BeginForm("Save", "Company", new AjaxOptions() {
    HttpMethod="Post", OnSuccess="closeForm" 
}, new {@id = "companyEditForm"})) {
    ....some edit fields......

    <input type="submit" value="Save & Next"/>
    <input type="submit" value="Save" />
}

使用“保存并下一步”按钮提交表单后,我想调用不同的 js 函数。因此,如果用户单击“保存”按钮,它应该提交表单然后调用“closeForm”javascript 函数。如果用户单击“保存并下一步”按钮,它应该提交表单,然后调用“nextForm”javascript 函数。有没有一种简单的方法可以实现这一目标?

4

3 回答 3

8

有没有一种简单的方法可以实现这一目标?

不,但是您可以让控制器操作传递在结果中单击的按钮。这可以作为 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 &amp; 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
        }
    }
}
于 2013-02-20T13:03:19.243 回答
1

您可以从 Ajax.BeginForm() 切换到 Html.BeginForm(),然后使用 JQuery 提交您的表单。

<script type="text/javascript">
$('#save').on('click', function () {
    var form = $('form');

    $.ajax({
        url: form.attr('action'),
        type: 'post',
        data: form.serialize(),
        success: function (result) {

            // call another function
        }
    });

    return false;
});

$('#saveAndEdit').on('click', function() {

    $.ajax({
        url: form.attr('action'),
        type: 'post',
        data: form.serialize(),
        success: function (result) {

            // call another function
        }
    });

    return false;
});
</script>
于 2013-02-20T13:39:47.593 回答
0

你也可以这样写:

@using (Ajax.BeginForm("Save", "Company", new AjaxOptions() {
    HttpMethod="Post", OnSuccess="closeForm" 
}, new { id = "companyEditForm" })) {

    <input type="submit" onclick="showNextForm = true;">Save & Next</button>
    <input type="submit" onclick="showNextForm = false;">Save</button>
}

...

var showNextForm = false;

function closeForm(data) {
    if(showNextForm) {
        nextForm();
    }
    else {
        // do your stuff
    }
}
于 2017-04-07T09:51:34.853 回答