asp.net-mvc – 使用Ajax.Beginform的RedirectToAction,意外结果

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – 使用Ajax.Beginform的RedirectToAction,意外结果前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下视图,其中包含一个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调用的以下Action方法: –

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,有没有办法强制我的应用程序不更新目标?

解决方法

操作成功时,返回要从操作方法重定向的URL:
public ActionResult ChangeDevicesSwitch(SwitchJoin s)
{
    try
    {
        ...
        return Json(new { RedirectUrl = Url.Action("Details",new { id = s.GeneralSwitchTo }) });
    }
    ...
}

并在创建成功:

function createsuccess(data) {
    if (data.RedirectUrl)
        window.location.href = data.RedirectUrl;
}
原文链接:https://www.f2er.com/aspnet/247792.html

猜你在找的asp.Net相关文章