0

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

我觉得哪里不对?

谢谢!

4

1 回答 1

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));
于 2013-05-13T23:17:50.353 回答