简体   繁体   English

不允许子操作执行重定向操作 - 使用ASP.NET MVC 2和Razor时出错

[英]Child actions are not allowed to perform redirect actions - error while working with ASP.NET MVC 2 and Razor

In _Layout.cshtml file I have following entry: _Layout.cshtml文件中,我有以下条目:

@Html.Action("LoadPagesStructure", "Page")

Inside PageController class, LoadPagesStructure methos looks following: 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();
}

Finally, my LoadPagesStructure.cshtml view looks like below: 最后,我的LoadPagesStructure.cshtml视图如下所示:

@inherits System.Web.Mvc.WebViewPage<dynamic>

<ul>
    @foreach (var page in View.Pages) {
    <li>        
        @Html.ActionLink(page, "Index", "Home")
    </li>
    }
</ul>

Unfortunately, an exception is thrown after execution: 不幸的是,执行后会抛出异常:

System.InvalidOperationException: Child actions are not allowed to perform redirect actions.

What is the way of creating links to my pages dynamically ? 动态创建指向我页面的链接的方式是什么?

PS: I know that I can do this like that: <a href="@page">@page</a> . PS:我知道我可以这样做: <a href="@page">@page</a> Nevertheless I think this is not the right way, because control of the routing is impossible here. 不过我认为这不是正确的方法,因为这里不可能控制路由。

Regards 问候

I think you might need to return a PartialView for your LoadPagesStructure() action. 我认为您可能需要为LoadPagesStructure()操作返回PartialView

Try this: 尝试这个:

[ChildActionOnly]
public ActionResult LoadPagesStructure()
{         
    ViewModel.Pages = new List<string>() {"page1", "page2", "page3"};
    return PartialView();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM