Ajax.BeginForm 根据提交值更改 AjaxOptions

Ajax.BeginForm change AjaxOptions depending on submit value

如何根据按下按钮的提交值更改 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();
}

当我post按下任何按钮时,我需要在返回新的PartialView之前更改AjaxOptions值。

begin 函数的 ajax 包含第二个参数(未记录)"request",这是一个具有表单请求信息的对象。

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

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