RenderAction
请求的控制器内部
@{Html.RenderAction("abc", "Controller"); }
如果出现问题,我正在尝试重定向到新的控制器,但是这个重定向会被嵌入,所以网站崩溃了。那么我如何重定向,因为我在下面做的代码不起作用?
我想知道它是否与ChildActionOnly
属性有关吗?
由于
[ChildActionOnly]
public ActionResult abc()
{
if(....)
{
// if all is OK return this view
return View("view", ViewModel
{
...
...
});
}
else
{
// This is the problem..
return RedirectToAction("LogOn", "controller");
}
}
答案 0 :(得分:2)
子操作无法执行重定向。
另一种方法是在主视图中使用JavaScript(下面的示例中使用的jQuery)来执行对方法的请求,如果失败将执行某些操作:
$.ajax({
url: '@Url.Action("SomeAction")'
}).fail(function() {
window.location = '@Url.Action("SomethingWentWrong")';
}).done(function(data) {
// display data
});
然后只需从SomeAction
返回500错误。