5

_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>。尽管如此,我认为这不是正确的方法,因为这里不可能控制路由。

问候

4

1 回答 1

3

PartialView我认为您可能需要为您的LoadPagesStructure()操作返回 a 。

尝试这个:

[ChildActionOnly]
public ActionResult LoadPagesStructure()
{         
    ViewModel.Pages = new List<string>() {"page1", "page2", "page3"};
    return PartialView();
}
于 2010-08-26T16:15:04.637 回答