Ajax.BeginForm根据提交值更改AjaxOptions

时间:2017-07-10 15:15:42

标签: asp.net-mvc-5 asp.net-ajax ajax.beginform unobtrusive-ajax

如何根据按下的按钮的提交值更改AjaxOptions的值。

在视图中:

@using (Ajax.BeginForm("function_name", "Controller",
new AjaxOptions
{
    HttpMethod = "POST",
    InsertionMode = InsertionMode.Replace,
    UpdateTargetId = "resultsLoad",
    OnBegin = "function_onBegin",
    OnFailure = "function_onFailure",
    OnSuccess = "function_onSuccess",
    OnComplete = "function_onComplete"
})) {
<button type="submit" id="btn_1" value="submit1" name="submit"></button>
<button type="submit" id="btn_2" value="submit2" name="submit"></button>
}

在控制器中:

[HttpPost]
public ActionResult function_name(IndexViewModel model, string submit)
{ 
    enter code...
    return PartialView();
}

当我通过按任意按钮发布时,我需要在返回新的PartialView之前更改AjaxOptions值。

1 个答案:

答案 0 :(得分:0)

ajax on begin函数包含第二个参数(未记录)&#34; request&#34;,这是一个带有表单请求信息的对象。

function function_onBegin(xhr, request) {
    //console.log(request);
    //request.data = submited form data as json string
    var requestData = request.data;    

    if (requestData.indexOf('anyCondition') >= 0) {
        //do something        
    } else {
        // do something else
    }
}

访问请求数据允许实现一些条件来完成所需的行为。