private PageHierarchyModel FindFirstModelWithValue(PageHierarchyModel root, string val)
 {
     if (root.Slug == val)
         return root;
     foreach (var child in root.Children)
     {
         var res = FindFirstModelWithValue(child, val);
         if (res != null)
             return res;
     }
     return null;
 }
        private PageHierarchyModel GetHierarchy(IEnumerable<Page> model)
        {
            PageHierarchyModel root = new PageHierarchyModel();

            PageHierarchyModel current = null;
            int level = 0;
            root.Level = level;
            foreach (Page p in model)
            {
                if (p.Path == null)
                {
                    root = new PageHierarchyModel(p,this.HttpContext);
                    current = root;
                    continue;
                }

                var splits = p.Path.Split(new char[] { '/' });
                current = FindFirstModelWithValue(root, splits.First());
                if (current == null)
                    current = root;
                else
                {
                    current = current.Parent;

                }

                var parts = p.Path.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

                foreach (var str in parts)
                {
                    var part = current.Children.FirstOrDefault(s => s.Slug == str);
                    if (part == null)
                    {
                        part = new PageHierarchyModel(p,HttpContext);
                        part.Level = parts.Length;
                        current.Children.Add(part);
                    }
                    current = part;
                }

            }
            return root;
        }
        public ActionResult List(Guid? parentId)
        {
            var pages = _pageService.ListPages(null,false, true, true);
            var hierarchy = new PageHierarchyModel[] { GetHierarchy(pages) };

            if (!Request.IsAjaxRequest())
                return View("List", hierarchy);
            return Json(hierarchy, JsonRequestBehavior.AllowGet);
        }