public void Traverse(List <menuTree> mlist, GenMenuList dto, menuTree m1) { foreach (var i in mlist) { // if this is a matching child if (i.key == dto.under.ToString()) { // add this as a child i.children.Add(m1); } i.folder = i.children.Count != 0; Traverse(i.children, dto, m1); } }
public List <menuTree> GetGenMenu(List <GenMenuList> list) { // create new menu tree List <menuTree> mlist = new List <menuTree>(); // hardcode manual example string genser = Newtonsoft.Json.JsonConvert.SerializeObject(list); Console.WriteLine("First List (nested correctly):"); Console.WriteLine(genser); foreach (GenMenuList dto in list) { // make a new branch of tree menuTree m1 = new menuTree(); m1.title = dto.mdisplay; m1.key = dto.keyno.ToString(); m1.children = new List <menuTree>(); // this is a parent folder if (dto.levelno == 0) { mlist.Add(m1); } else { Traverse(mlist, dto, m1); } } string chkmlist = Newtonsoft.Json.JsonConvert.SerializeObject(mlist); Console.WriteLine("\nSecond List (catching the parents):"); Console.WriteLine(chkmlist); return(mlist); //return "returned"; //return Json(mlist, JsonRequestBehavior.AllowGet); }