namespace MyProject.Helpers
{
public static class TreeView
{
private static IEnumerable<Category> category;
public static MvcHtmlString DisplayTree(this HtmlHelper html, IEnumerable<Category> collection)
{
category = collection;
return NewMethod(null);
}
private static MvcHtmlString 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();
NewMethod(item.Id);
}
}
return new MvcHtmlString(ul.ToString());
}
}
}
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 "";
}
}