在_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")
}
不幸的是,执行后会抛出异常:
System.InvalidOperationException: Child actions are not allowed to perform redirect actions.
动态创建指向我页面的链接的方式是什么?
PS:我知道我可以这样做:@page
.不过我认为这不是正确的方法,因为这里不可能控制路由.
问候