用c#递归输出ul-list

时间:2013-05-13 20:32:29

标签: c# html recursion

我正在尝试将表格中的结果输出为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>"

我觉得哪里不正确?

谢谢!

1 个答案:

答案 0 :(得分:0)

我没有检查您的代码有什么问题,而是检查了这个实现。

public string GetTree(List<Link> linkList) {
  var sb = new StringBuilder();
  sb.Append("<ul>");

  foreach (var link in linkList) {
    if (!link.ParentId.HasValue) {
      sb.Append("<li>");
      sb.Append(link.Title);
      sb.Append(GetChildren(linkList, link.Id));
      sb.Append("</li>");
    }
  }

  sb.Append("</ul>");
  return sb.ToString();
}

private string GetChildren(List<Link> linkList, int parentId) {
  var sb = new StringBuilder();

  var childList = from m in linkList
                  where m.ParentId == parentId
                  orderby m.Id
                  select m;

  if (childList.Count() > 0) {
    sb.Append("<ul>");
    foreach (var link in childList) {
      sb.Append("<li>");
      sb.Append(link.Title);
      sb.Append(GetChildren(linkList, link.Id));
      sb.Append("</li>");
    }
    sb.Append("</ul>");
  }
  return sb.ToString();
}

navigation = new HtmlString(GetTree(linkList));