簡體   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

_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> 不過我認為這不是正確的方法,因為這里不可能控制路由。

問候

我認為您可能需要為LoadPagesStructure()操作返回PartialView

嘗試這個:

[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