在MVC中使用递归局部视图会引发堆栈为空的InvalidOperationException [英] Using recursive partial view in MVC raises Stack empty InvalidOperationException
问题描述
我想创建一个带有递归元素的递归菜单.我的第一种方法是使用部分视图以便重复使用代码.A,当我运行代码时,它会引发"System.InvalidOperationException"堆栈为空"错误.
I want to create a recursive menu with recursive elements. My first approach was to use Partial Views in order to re-use code.. Alas, when I run the code, it throws a "System.InvalidOperationException" "Stack empty" error.
我正在使用AutoMapper映射来自Web服务的数据,这将导致以下实体:
I am mapping data from a web service with AutoMapper, which results in the following entity:
public interface INavigationItemContract
{
int Id { get; set; }
string Title { get; set; }
int ParentId { get; set; }
string Url { get; set; }
bool DisplayInMenu { get; set; }
decimal SortOrder { get; set; }
IEnumerable<IPageBaseContract> Pages { get; set; }
IEnumerable<INavigationItemContract> Sites { get; set; }
}
用于初始化菜单的局部视图(_Menu.cshtml):
The partial view to initialize the menu (_Menu.cshtml):
@model IEnumerable<INavigationItemContract>
<div class="navbar navbar-default" role="navigation">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a href="/">Home</a></li>
@foreach (var navigationItem in Model)
{
@Html.Partial("_MenuItem", navigationItem);
}
</ul>
</div>
</div>
显示菜单项(_MenuItem.cshtml)的部分:
The partial to display the menu item (_MenuItem.cshtml):
@model INavigationItemContract
@if (Model.Pages != null && Model.Pages.Any())
{
if (Model.Pages.Count() == 1 || !Model.DisplayInMenu)
{
// Only one page, so this will be the default.
// Other option is that the pages should not be displayed, so then we'll only display the link to the site.
<li><a href="@Model.Url">@Model.Title</a></li>
}
else
{
<li>
<a href="#">@Model.Title</a>
<ul class="dropdown-menu">
@foreach (var page in Model.Pages)
{
<li><a href="@page.RelativeUrl">@page.Title</a></li>
}
@foreach (var site in Model.Sites)
{
@Html.Partial("_MenuItem", site)
}
</ul>
</li>
}
}
更换
@Html.Partial("_MenuItem", site)
与
<li>@site.Title</li>
所有内容都像一种魅力(除了它不是期望的结果之外).
everything works like a charm (except for the fact that it is not the desired result).
我也尝试了以下方法:
- 创建DisplayTemplate
- 使用Html.RenderPartial代替Html.Partial
该异常发生在_MenuItem.cshtml中:
The exception occurs in the _MenuItem.cshtml:
@Html.Partial("_MenuItem", site)
第一次调用该部分时(在_Menu.cshtml中),仅在_MenuItem.cshtml自身中,它不会发生.
It does not occur the first time the partial is called (in the _Menu.cshtml), only in the _MenuItem.cshtml itself, when it is calling itself.
为什么会出现此异常?可能的解决方案是什么?
Why am I getting this exception? And what might be the solution?
推荐答案
我找到了一种解决方法.异常仍然使我感到困惑,但是我可以继续渲染菜单(即使我没有能力在辅助代码上设置断点).
I have found a workaround. The exception still puzzles me, but I can continue to render the menu (even though I do not have the ability to set a breakpoint on the helper code)..
@model IEnumerable
@model IEnumerable
@helper RecursiveMenuItem(INavigationItemContract menuItem)
{
if (menuItem.Pages != null && menuItem.Pages.Any())
{
if (menuItem.Pages.Count() == 1 || !menuItem.DisplayInMenu)
{
// Only one page, so this will be the default.
// Other option is that the pages should not be displayed, so then we'll only display the link to the site.
<li><a href="@menuItem.Url">@menuItem.Title</a></li>
}
else
{
<li>
<a href="#">@menuItem.Title</a>
<ul class="dropdown-menu">
@foreach (var page in menuItem.Pages)
{
<li><a href="@page.Url">@page.Title</a></li>
}
@foreach (var site in menuItem.Sites)
{
@RecursiveMenuItem(site)
}
</ul>
</li>
}
}
}
<div role="navigation">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a href="">Home</a></li>
@foreach (var menuItem in Model)
{
@RecursiveMenuItem(menuItem)
}
</ul>
</div>
</div>
这篇关于在MVC中使用递归局部视图会引发堆栈为空的InvalidOperationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!