有没有办法追加ul class =" nav nav-third-level background-white collapse"只有第一次循环到达那里?我试过,bool和一个计数器,但显然它每次都被初始化,我试着跳过增量,但这样它甚至不能初始化它:)
我试图获取以下HTML代码:
<li class="">
<a href="#"><i class="fa fa-fw"></i>First Level<span class="fa arrow"></span></a>
<ul class="nav nav-second-level background-white collapse">
<li>
<a href="#">Second Level1</a>
<ul class="nav nav-third-level background-white collapse">
<li>
<a href="#">Third Level1</a>
</li>
<li>
<a href="#">Third Level2</a>
</li>
</ul>
</li>
<li>
<a href="#">Second Level2</a>
<ul class="nav nav-third-level background-white collapse">
<li>
<a href="#">Third Level4</a>
</li>
<li>
<a href="#">Third Level5</a>
</li>
</ul>
</li>
</ul>
</li>
这是我的C#代码:
private static string RenderMenuRecursive(ItemMenu menu, int level)
{
StringBuilder sb = new StringBuilder();
if (level > 0)
{
int firstIteration = 0; // This will be initialized as 0 every time :\
if (level == 1)
{
sb.AppendLine("<li class=\"\"><a href=\"#\"><i class=\"fa fa-fw\"></i>"+ menu.name + "<span class=\"fa arrow\"></span></a>");
}
if (level == 2)
{
sb.AppendLine("<ul class=\"nav nav-second-level background-white collapse\"><li><a href=\"#\">" + menu.name + "</a>");
}
if (level == 3)
{
if (firstIteration == 0)
{
sb.AppendLine("<ul class=\"" + "nav nav-third-level background-white collapse" + "\">");
firstIteration++;
}
sb.AppendLine("<li><a href=\"#\">" + menu.name + "</a></li>");
}
}
level++;
foreach (ItemMenu subMenu in menu.subMenuItems)
{
sb.AppendLine(RenderMenuRecursive(subMenu, level));
}
return sb.ToString();
}
这就是我实际得到的:
<li class=""><a href="#"><i class="fa fa-fw"></i>First Level<span class="fa arrow"></span></a>
<ul class="nav nav-second-level background-white collapse"><li><a href="#">Second Level1</a>
<ul class="nav nav-third-level background-white collapse">
<li><a href="#">Third Level1</a></li>
<ul class="nav nav-third-level background-white collapse">
<li><a href="#">Third Level2</a></li>
<ul class="nav nav-second-level background-white collapse"><li><a href="#">Second Level2</a>
<ul class="nav nav-third-level background-white collapse">
<li><a href="#">Third Level3</a></li>
<ul class="nav nav-third-level background-white collapse">
<li><a href="#">Third Level4</a></li>
答案 0 :(得分:1)
您在错误的位置定义和初始化变量。由于您为每次迭代调用方法,因此您的变量将获得一个新的范围,并在每次迭代中进行初始化。
如果你想在for-each循环中调用方法,你需要让方法知道你在哪个迭代中。例如通过引入一个参数:
private static string RenderMenuRecursive(ItemMenu menu, int level, bool firstIteration)
{
[...]
if (level == 3)
{
if (firstIteration)
{
sb.AppendLine("<ul class=\"" + "nav nav-third-level background-white collapse" + "\">");
firstIteration++;
}
sb.AppendLine("<li><a href=\"#\">" + menu.name + "</a>");
}
[...]
var firstIteration = true;
foreach (ItemMenu subMenu in menu.subMenuItems)
{
sb.AppendLine(RenderMenuRecursive(subMenu, level, firstIteration));
firstIteration = false;
}
return sb.ToString();
}
需要说明的是,此问题与渲染,StringBuilders,HTML或MVC无关。这只是如何使用嵌套(递归)方法调用和状态...
的问题答案 1 :(得分:1)
问题在于 int firstIteration = 0; 它总是设置为0,因为它不是静态成员(你应该避免使用静态成员)
你应该在你的方法RenderMenuRecursive(subMenu,level, renderUl )中添加param并移动
int firstIteration = 0; //每次
初始化为0到
int firstIteration = 0;
foreach (ItemMenu subMenu in menu.subMenuItems)
{
sb.AppendLine(RenderMenuRecursive(subMenu, level, firstInteration == 0));
firstIteration++;
}
还要考虑方法名称更改,因为没有递归调用。