加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 04:17:08 所属栏目:asp.Net 来源:网络整理
导读:我有以下视图,其中包含一个Ajax.BeginForm: – @using (Ajax.BeginForm("ChangeDevicesSwitch","Switch",new AjaxOptions{ InsertionMode = InsertionMode.InsertBefore,UpdateTargetId = "result",LoadingElementId = "progress2",HttpMethod= "POST",OnSu
我有以下视图,其中包含一个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;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读