本文介绍了两人在Ajax.BeginForm提交按钮。需要调用JS不同功能的onSuccess的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的MVC页上的一个Ajax形式,具有两个独立的提交按钮...

  @using(Ajax.BeginForm(保存,公司,新AjaxOptions(){
    列举HTTPMethod =邮报,的onSuccess =closeForm
},{新@id =companyEditForm})){
    ....一些编辑字段......    <输入类型=提交值=保存并放;下一步/>
    <输入类型=提交值=保存/>
}

我想形式与提交后,调用不同的js函数保存安培;下一步按钮。因此,如果用户点击保存按钮,就应该再提交表单称之为closeFormjavascript函数。如果用户点击储存并下一步按钮,就应该提交表单,然后调用javascript函数nextForm。是否有实现这一目标的一个简单的方法?


解决方案

No, but you could have the controller action pass the button that was clicked in the result. This could be done either as a Json property (if you are returning JSON) or it could also be a custom response HTTP header.

And then inside your success callback (which can only be one) you could retrieve this value in order to know which button was clicked and act accordingly.

So, start by giving a name to your submit button so that you know which one was clicked:

@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>
}

And then inside your controller action

[HttpPost]
public ActionResult Save(MyViewModel model)
{
    Response.AppendHeader("X-Button", Request["btn"]);

    ... your usual processing
}

and finally inside your onSucecss callback:

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
        }
    }
}

这篇关于两人在Ajax.BeginForm提交按钮。需要调用JS不同功能的onSuccess的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 22:46