gpt4 book ai didi

asp.net-mvc - RedirectToAction 与 Ajax.Beginform ,意外结果

转载 作者:行者123 更新时间:2023-12-02 07:03:07 26 4
gpt4 key购买 nike

我有以下 View ,其中包含 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 到达时,整个 View 将显示在当前 View 内!那么有没有办法在返回 RedirecttoAction 时强制我的应用程序不更新目标?

最佳答案

操作成功时,从操作方法返回要重定向到的 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;
}

关于asp.net-mvc - RedirectToAction 与 Ajax.Beginform ,意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23786838/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com