8

我有以下视图,其中包含一个 Ajax.BeginForm:-

@using (Ajax.BeginForm("ChangeDevicesSwitch", "Switch", new AjaxOptions

{
    InsertionMode = InsertionMode.InsertBefore,
    UpdateTargetId = "result",
    LoadingElementId = "progress2",
    HttpMethod= "POST"
    ,
    OnSuccess = "createsuccess",
    OnFailure = "createfail"




}))
//code goes here
<p><img src="~/Content/Ajax-loader-bar.gif" class="loadingimage" id="progress2" /></p>
<div id ="result"></div>

以及将从 Ajax.Bginform 调用的以下操作方法:-

public ActionResult ChangeDevicesSwitch(SwitchJoin s)

        {//code goes here
            try
            {
                var count = repository.changeDeviceSwitch(s.Switch.SwitchID, (Int32)s.GeneralSwitchTo, User.Identity.Name.Substring(User.Identity.Name.IndexOf("\\") + 1));
                repository.Save();
                return RedirectToAction("Details", new { id = s.GeneralSwitchTo });

            }
            catch (Exception e)

            {
                return Json(new { IsSuccess = "custome", description = "Error occurred. Please check...." }, JsonRequestBehavior.AllowGet);
            }



        }

当 Ajax.BeginForm 返回成功时将运行的脚本是:-

function createsuccess(data) {
    if (data.IsSuccess == "Unauthorized") {

        jAlert(data.description, 'Unauthorized Access');
    }
    else if (data.IsSuccess == "False") {

        jAlert('Error Occurred. ' + data.description, 'Error');
    }
    else if (data.IsSuccess == "custome") {

        alert(data.description);

    }
    else  {
        jAlert('Record added Successfully ', 'Creation Confirmation');
    }

}

目前我面临的一个问题是,当达到 RedirectToAction 时,整个视图将显示在当前视图中!那么如果返回 RedirecttoAction 有没有办法强制我的应用程序不更新目标?

4

2 回答 2

17

操作成功时,从 action 方法返回您要重定向到的 url:

public ActionResult ChangeDevicesSwitch(SwitchJoin s)
{
    try
    {
        ...
        return Json(new { RedirectUrl = Url.Action("Details", new { id = s.GeneralSwitchTo }) });
    }
    ...
}

并且在createsuccess

function createsuccess(data) {
    if (data.RedirectUrl)
        window.location.href = data.RedirectUrl;
}
于 2014-05-21T15:34:20.403 回答
0

就我而言,以下方法有效

function OnSuccess(data) {
    var json;
        if (data.responseText instanceof Object)
            json = data.responseText;
        else
            json = $.parseJSON(data.responseText);

        if (json.RedirectUrl)
            window.location.href = json.RedirectUrl;
}
于 2017-05-29T16:03:11.363 回答