public class Category
{
public string Name{ get; set; }
public Users User { get; set; }
}
public static class TreeView
{
private static IEnumerable<Category> category;
public static MvcHtmlString DisplayTree(this HtmlHelper html, IEnumerable<Category> collection)
{
category = collection;
return new MvcHtmlString(NewMethod(null));
}
private static string NewMethod(int? parentId)
{
TagBuilder ul = new TagBuilder("ul");
if (category.Any(x => x.ParentCategoryId == parentId))
{
foreach (var item in category.Where(x => x.ParentCategoryId == parentId))
{
TagBuilder li = new TagBuilder("li");
li.SetInnerText(item.Name);
ul.InnerHtml += li.ToString();
ul.InnerHtml += NewMethod(item.Id);
}
return ul.ToString();
}
return "";
}
}