![](/img/trans.png)
[英]calling return RedirectToAction(“Activity”) outside controller
[英]How Do You Call a Controller Action That Will Return to The Calling Controller But Also Can Have A RedirectToAction
我的 .Net Core 3.1 应用程序中有一个控制器,它调用另一个控制器操作。 在我在被调用的控制器中添加RedirectToAction
之前,一切正常。 我现在希望该控制器在适当的情况下重定向到错误页面,否则返回到原始控制器中调用它的位置。
这是我的控制器中的代码:
public ActionResult Edit(IFormCollection collection, string saveClose)
{
//do stuff
LoadArrayDataForDB(Id, collection["names"].ToString(), collection["places"].ToString(), collection["countries"].ToString());
//do more stuff
return View(IVM);
}
//This was the Original LoadArrayDataForDB action
public void LoadArrayDataForDB(string id, string names, string places, string countries)
{
//add error checking
_adoSqlService.DeleteAffectedNames(id);
_adoSqlService.DeleteSelectedPlaces(id);
_adoSqlService.DeleteCountries(id);
}
//This is how I need to change the LoadArrayDataForDB action
public void LoadArrayDataForDB(string id, string names, string places, string countries)
{
//add error checking
string done = _adoSqlService.DeleteAffectedNames(id);
if (done != "Successful")
{
return RedirectToAction("Error", "Home", new { errordescription = "Unable to delete names" });
}
done = _adoSqlService.DeleteAffectedPlaces(id);
if (done != "Successful")
{
return RedirectToAction("Error", "Home", new { errordescription = "Unable to delete places" });
}
done = _adoSqlService.DeleteAffectedCountries(id);
if (done != "Successful")
{
return RedirectToAction("Error", "Home", new { errordescription = "Unable to delete countries" });
}
//etc....
//Just return back to next step in the Edit action
}
我该如何做我需要做的事情?
将您的加载功能更改为此
[NonAction]
private string LoadArrayDataForDB( id, string names, string places, string countries)
{
//add error checking
string done = _adoSqlService.DeleteAffectedNames(id,names); // I guess you
// have to use names too
if (done != "Successful") return "Unable to delete names";
done = _adoSqlService.DeleteAffectedPlaces(id,places);
if (done != "Successful")return "Unable to delete places";
done = _adoSqlService.DeleteAffectedCountries(id,contries);
if (done != "Successful") return "Unable to delete countries";
//etc....
return string.Empty
)
or if you want to try all of them before return you can use this
var errMsg=string.Empty;
string done = _adoSqlService.DeleteAffectedNames(id,names); // I guess you
// have to use names too
if (done != "Successful") errMsg= "Unable to delete names";
done = _adoSqlService.DeleteAffectedPlaces(id,places);
if (done != "Successful") errMsg+= " Unable to delete places";
done = _adoSqlService.DeleteAffectedCountries(id,contries);
if (done != "Successful") errMsg+= "Unable to delete countries";
if(!string.IsNullOrEmpty(errMsg) return errMsg;
//etc....
return string.Empty
改变你的行动:
public ActionResult Edit(IFormCollection collection, string saveClose)
{
//do stuff
var errorDescription= LoadArrayDataForDB(intid, collection["names"].ToString(), collection["places"].ToString(), collection["countries"].ToString()))
if (! string.IsNullOrEmpty(errorDescription) )
return RedirectToAction("Error", "Home", new { errorDescription });
//do more stuff
return View(IVM);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.