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

人盡茶涼 提交于 2019-12-01 17:14:06

问题


In _Layout.cshtml file I have following entry:

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

Inside PageController class, LoadPagesStructure methos looks following:

[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:

@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>. Nevertheless I think this is not the right way, because control of the routing is impossible here.

Regards


回答1:


I think you might need to return a PartialView for your LoadPagesStructure() action.

Try this:

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


来源:https://stackoverflow.com/questions/3573554/child-actions-are-not-allowed-to-perform-redirect-actions-error-while-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!