不允许子操作执行重定向操作 – 使用ASP.NET MVC 2和Razor时出错
在_Layout.cshtml文件中,我有以下条目:
@Html.Action("LoadPagesStructure", "Page")
在PageController类中, LoadPagesStructure方法如下:
[ChildActionOnly] /* this attribute indicates that an action should not be invoked as a result of a user request (by url) */ public ActionResult LoadPagesStructure() { ViewModel.Pages = new List() {"page1", "page2", "page3"}; return View(); }
最后,我的LoadPagesStructure.cshtml视图如下所示:
@inherits System.Web.Mvc.WebViewPage @foreach (var page in View.Pages) { - @Html.ActionLink(page, "Index", "Home")
}
不幸的是,执行后会抛出exception:
System.InvalidOperationException: Child actions are not allowed to perform redirect actions.
动态创建链接到页面的方式是什么?
PS:我知道我可以这样做: https://stackoverflow.com/questions/3573554/child-actions-are-not-allowed-to-perform-redirect-actions-error-while-working/@page
。 不过我认为这不是正确的方法,因为这里不可能控制路由。
问候
我认为您可能需要为LoadPagesStructure()
操作返回PartialView
。
试试这个:
[ChildActionOnly] public ActionResult LoadPagesStructure() { ViewModel.Pages = new List() {"page1", "page2", "page3"}; return PartialView(); }