RedirectToAction与Ajax.Beginform,意想不到的效果 [英] RedirectToAction with Ajax.Beginform , unexpected results

查看:171
本文介绍了RedirectToAction与Ajax.Beginform,意想不到的效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的看法,其中包含一个Ajax.BeginForm: -

I have the following view , which contains an 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被称为: -

and the following Action Method which will be called from the 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返回成功,这将运行该脚本是: -

The script which will run when the Ajax.BeginForm return success is :-

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不更新目标?

currently i am facing a problem is that when the RedirectToAction is reach ,, the whole view will be displayed inside the current view!! so is there a way to force my application not to update the target if an RedirecttoAction is returned ?

推荐答案

返回的网址你想重定向到从操作方法时,操作成功:

Return the url you want to make a redirect to from the action method when the operation succeeded:

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

这篇关于RedirectToAction与Ajax.Beginform,意想不到的效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆