Is it possible to output the following HTML unordered list using recursion.
<ul>
<li>1
<ul>
<li>5
<ul>
<li>8</li>
<li>9</li>
</ul>
</li>
<li>6</li>
</ul>
</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>7</li>
</ul>
Where the original data is is held in the following list
List<Page> pages = new List<Page>();
pages.Add(new Page { Id = 1, pageId = 1, parentPageId = 0 });
pages.Add(new Page { Id = 2, pageId = 2, parentPageId = 0 });
pages.Add(new Page { Id = 3, pageId = 3, parentPageId = 0 });
pages.Add(new Page { Id = 4, pageId = 4, parentPageId = 0 });
pages.Add(new Page { Id = 5, pageId = 5, parentPageId = 1 });
pages.Add(new Page { Id = 6, pageId = 6, parentPageId = 1 });
pages.Add(new Page { Id = 7, pageId = 7, parentPageId = 0 });
pages.Add(new Page { Id = 8, pageId = 8, parentPageId = 5 });
pages.Add(new Page { Id = 9, pageId = 9, parentPageId = 5 });
where Page is
public class Page
{
public int Id { get; set; }
public int pageId { get; set; }
public int parentPageId { get; set; }
}
Here's what I have so far which of course doesn't completely work...
foreach (var page in pages)
{
Console.WriteLine("<ul><li>{0}", page.pageId);
WriteMenu(pages, page.pageId, page.parentPageId);
}
public static void WriteMenu(List<Page> pages, int pageId, int parentPageId)
{
foreach (var page in pages)
{
if (page.parentPageId == pageId)
{
Console.WriteLine("<ul><li>{0}", page.pageId);
WriteMenu(pages, page.pageId, page.parentPageId);
}
}
}