我有这段代码
<div class="round-3-top">
<div class="round-2-top">
<div class="round-1-top"></div>
<div class="round-1-bottom"></div>
</div>
<div class="round-2-bottom">
<div class="round-1-top"></div>
<div class="round-1-bottom"></div>
</div>
</div>
<div class="round-3-bottom">
<div class="round-2-top">
<div class="round-1-top"></div>
<div class="round-1-bottom"></div>
</div>
<div class="round-2-bottom">
<div class="round-1-top"></div>
<div class="round-1-bottom"></div>
</div>
</div>
但我想动态生成,我是怎么做到的?
例:我有4轮,这将是生成的代码
<div class="round-4-top">
<div class="round-3-top">
<div class="round-2-top">
<div class="round-1-top"></div>
<div class="round-1-bottom"></div>
</div>
<div class="round-2-bottom">
<div class="round-1-top"></div>
<div class="round-1-bottom"></div>
</div>
</div>
<div class="round-3-bottom">
<div class="round-2-top">
<div class="round-1-top"></div>
<div class="round-1-bottom"></div>
</div>
<div class="round-2-bottom">
<div class="round-1-top"></div>
<div class="round-1-bottom"></div>
</div>
</div>
</div>
<div class="round-4-bottom">
<div class="round-3-top">
<div class="round-2-top">
<div class="round-1-top"></div>
<div class="round-1-bottom"></div>
</div>
<div class="round-2-bottom">
<div class="round-1-top"></div>
<div class="round-1-bottom"></div>
</div>
</div>
<div class="round-3-bottom">
<div class="round-2-top">
<div class="round-1-top"></div>
<div class="round-1-bottom"></div>
</div>
<div class="round-2-bottom">
<div class="round-1-top"></div>
<div class="round-1-bottom"></div>
</div>
</div>
</div>
我尝试在MVC C#中使用TagBuilder,但我不能这样做。应该发生的是,如果你是3轮,添加他应该进入每个div内部就像上面的例子。我知道如何开发它?
答案 0 :(得分:1)
您可以使用返回MVCHtmlString的MVC Helpers执行此操作。助手类如下。递归是完美的。 辅助类
public static class Helpers
{
public static MvcHtmlString GetStructure(int size)
{
return MvcHtmlString.Create(GetRecursiveStructure(size));
}
private static string GetRecursiveStructure(int size)
{
if (size == 0)
{
return "";
}
else
{
TagBuilder topTag = new TagBuilder("div");
topTag.AddCssClass(string.Format("round-{0}-top", size));
topTag.InnerHtml = GetRecursiveStructure(size - 1);
TagBuilder bottomTag = new TagBuilder("div");
bottomTag.AddCssClass(string.Format("round-{0}-bottom", size));
bottomTag.InnerHtml = GetRecursiveStructure(size - 1);
return topTag.ToString() + bottomTag.ToString();
}
}
}
在您看来,请执行以下操作:
@Helpers.GetStructure(4)