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

In the _Layout.cshtml file , I have the following entry:

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

Inside the PageController class , LoadPagesStructure looks like this:

[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 view of LoadPagesStructure.cshtml is as follows:

@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.

How can I dynamically create links to my pages?

PS: I know I can do it like this: <a href="@page">@page</a>. However, I think this is the wrong way, because routing control is not possible here.

Hi

+5
1

, PartialView LoadPagesStructure().

:

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

All Articles