在_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<string>() {"page1", "page2", "page3"};
return View();
}
最后,我的LoadPagesStructure.cshtml 视图如下所示:
@inherits System.Web.Mvc.WebViewPage<dynamic>
<ul>
@foreach (var page in View.Pages) {
<li>
@Html.ActionLink(page, "Index", "Home")
</li>
}
</ul>
不幸的是,执行后抛出异常:
System.InvalidOperationException: Child actions are not allowed to perform redirect actions.
动态创建指向我的页面的链接的方式是什么?
PS:我知道我可以这样做:<a href="@page">@page</a>
。尽管如此,我认为这不是正确的方法,因为这里不可能控制路由。
问候