I'm trying to extrapolate and transfer some answered questions on recursion into one of my projects.
Answered question on recursion that I'm referencing:
asp-net-mvc-4-generating-a-treeview-with-recursive-partial-view
However, I'm having trouble grasping the concept and applying it in my project.
Here is my attempt.
First, I created a class called Models:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MVCMnetWebsite.Models
{
public class Category
{
public int TAB_ITEM_ID { get; set; }
public string TAB_ITEM_NAME { get; set; }
public int? TAB_ITEM_PARENT_ID { get; set; }
public string URL { get; set; }
public string WINDOW_NAME { get; set; }
public string TOOL_TIP { get; set; }
public string ACCESS { get; set; }
public DateTime? START_DATE { get; set; }
public DateTime? END_DATE { get; set; }
public int? LASTUPDATE_BY { get; set; }
public int? SNAC_TYPES_OBJID { get; set; }
public byte? ACTIVE { get; set; }
public string ICON { get; set; }
}
public class SeededCategories
{
public int? Seed { get; set; }
public IList<Category> Categories { get; set; }
}
}
Then, I created a controller called TreeController:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCMnetWebsite.Models;
namespace MVCMnetWebsite.Controllers
{
public class TreeController : Controller
{
public ActionResult Index()
{
var categories = Session["_menu"] as IList<Category>;
SeededCategories model = new SeededCategories { Seed = null, Categories = categories };
return View(model);
}
}
}
Then, I created a partial view called _TreeCategories:
@model MVCMnetWebsite.Models.SeededCategories
@using MVCMnetWebsite.Models
@{var menuList = Session["_menu"] as List<Category>;}
@{int appID = 72;} @*PKEY OF APP - LOCATED: [INTRAWEB].[dbo][DATA_SN_ACCESS_CONTROL_TYPES]*@
@foreach (var topMenuList in menuList.Where(x => x.TAB_ITEM_PARENT_ID == null && x.SNAC_TYPES_OBJID == appID).Select(x => x).ToList())
{
<li>
<a href="#"><span><i class="@topMenuList.ICON"></i> @topMenuList.TAB_ITEM_NAME</span></a>
<ul>
@foreach (var subMenuList in menuList.OrderBy(x => x.TAB_ITEM_NAME).Where(x => x.TAB_ITEM_PARENT_ID == topMenuList.TAB_ITEM_ID).Select(x => x).ToList())
{
if (menuList.Any(x => x.TAB_ITEM_PARENT_ID == subMenuList.TAB_ITEM_ID))
{
<li>
<a href="#"><span><i class="fa fa-toggle-right"></i> @subMenuList.TAB_ITEM_NAME</span></a>
<ul>
@foreach (var subChildMenuList in menuList.Where(x => x.TAB_ITEM_PARENT_ID == subMenuList.TAB_ITEM_ID).Select(x => x).ToList())
{
<li class="urlLink">
<a name="@subChildMenuList.URL" href="">@subChildMenuList.TAB_ITEM_NAME</a>
</li>
}
</ul>
</li>
}
else
{
<li class="urlLink"><a name="@subMenuList.URL" href="">@subMenuList.TAB_ITEM_NAME </a></li>
}
}
</ul>
</li>
}
and in my _layout.cshtml, I have:
@Html.Partial("_TreeCategories", Model)
From the code, shown above, I can only show items up to the 2nd child. How do I use recursion to show items up to the Nth child?
EDITED:
@if (Model.Categories.Where(s => s.TAB_ITEM_PARENT_ID == Model.Seed).Any())
{
<ul>
@foreach (var node in Model.Categories)
{
if (node.TAB_ITEM_PARENT_ID == Model.Seed)
{
SeededCategories inner = new SeededCategories { Seed = node.TAB_ITEM_ID, Categories = Model.Categories };
<li>
<a href="[email protected]_ITEM_ID">@node.TAB_ITEM_NAME</a>
@Html.Partial("_TreeCategories", inner)
</li>
}
}
</ul>
}
The above code works but it needs to be tweaked in order to render the proper style. Additionally, the code above was taken from the link mentioned at the top. I just dont know where to add my HTML structure - meaning: The above code starts with an unordered list where mine starts with a list item The HTML is different and I don't see the pattern and how to transfer this to my situation.
CategoryVMwhich contains a propertyList<CategoryVM> ChildCategories