我正在尝试将表中的结果作为 html ul-list 输出。我将结果绑定到 List<>。以最简单的方式,它可以看起来像这样。(我正在模拟绑定,但重要的是要知道我按此顺序对列进行排序)
List<Link> linkList = new List<Link>();
linkList.Add(new Link() { Id = 1, ParentId = null, Title = "Sport" });
linkList.Add(new Link() { Id = 2, ParentId = 1, Title = "Football" });
linkList.Add(new Link() { Id = 3, ParentId = 1, Title = "Handball" });
linkList.Add(new Link() { Id = 4, ParentId = 1, Title = "Golf" });
在此之后,我调用我的递归方法。
public HtmlString navigation { get; set; }
navigation = CreateNavigation(linkList);
protected static HtmlString CreateNavigation1(List<Link> linkList, string result = "", int? index = null)
{
int maxIndex = linkList.Count() - 1;
if (!index.HasValue)
index = maxIndex;
else
index--;
Link selectedLink;
if (index != 0)
{
selectedLink = linkList[index.Value];
if (index != maxIndex)
{
if (linkList[index.Value + 1].ParentId != selectedLink.ParentId)
{
return new HtmlString("<li id=\"" + selectedLink.Id + "\">" + selectedLink.Title + "<ul>" + CreateNavigation1(linkList, result, index) + "</ul></li>");
}
else
{
return new HtmlString(CreateNavigation1(linkList, result, index) + "<li id=\"" + selectedLink.Id + "\">" + selectedLink.Title + "</li>");
}
}
else
{
return new HtmlString(CreateNavigation1(linkList, result, index) + "<li id=\"" + selectedLink.Id + "\">" + selectedLink.Title + "</li>");
}
}
else
{
selectedLink = linkList[index.Value];
if (linkList[index.Value + 1].ParentId.HasValue && linkList[index.Value + 1].ParentId.Value == selectedLink.Id)
{
return new HtmlString("<ul><li>" + selectedLink.Title + "<ul>" + result + "</ul></li></ul>");
}
else
{
return new HtmlString("<ul><li>" + selectedLink.Title + "</li>" + result + "</ul>");
}
}
}
当我单步运行时,我认为它应该运行,但是当我查看输出时,我不明白出了什么问题。这是结果
<ul><li>Sport<ul></ul></li></ul><li id="2">Football</li><li
id="3">Handball</li><li id="4">Golf</li>
我的愿望是它应该看起来像这样
<ul><li>Sport<ul><li id="2">Football</li><li id="3">Handball</li><li
id="4">Golf</li></ul></li></ul>
结果如何附加在字符串之后而不是在其中。我说
<ul><li>" + selectedLink.Title + "</li>" + append it here!(result) + "</ul>"
我觉得哪里不对?
谢谢!