儿童的行为是不允许进行重定向操作 - 误差ASP.NET MVC 2和剃刀工作时 [英] 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 methos看起来如下:
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();
}
最后,我的 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:我知道我能做到这一点这样的:&LT; A HREF =@页&GT; @网页&LT; / A&GT;
。不过我认为这是不正确的做法,因为路由的控制是不可能在这里。
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.
问候
推荐答案
我想你可能需要返回一个 PartialView
您 LoadPagesStructure()
操作。
I think you might need to return a PartialView
for your LoadPagesStructure()
action.
试试这个:
[ChildActionOnly]
public ActionResult LoadPagesStructure()
{
ViewModel.Pages = new List<string>() {"page1", "page2", "page3"};
return PartialView();
}
这篇关于儿童的行为是不允许进行重定向操作 - 误差ASP.NET MVC 2和剃刀工作时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!