Quantcast
Viewing all articles
Browse latest Browse all 9386

JSON Models with Children

I'm still learning .NET (and .NET Core), and am having problems wrapping my brain around something. So I'm hoping someone can give me a nudge...

I'm trying to return some JSON data from a Controller (it's some info from an LDAP query) so I can populate a JQuery tree item on the client side. I can get top-level objects to populate just fine within my tree with my Controller returning something like this (the values are just hardcoded to show what I'm doing):

List<TreeViewItem> treeData = new List<TreeViewItem>()
{
   new TreeViewItem() { Title = "MyFirstObject", Folder = true, Children = false },
   new TreeViewItem() { Title = "SomeOtherItem", Folder = true, Children = false },
   new TreeViewItem() { Title = "AnotherRandom", Folder = true, Children = false },
};

return Json(treeData);


But I'm running into issues with children objects for the tree. Here is a hardcoded sample of what format the tree plug-in (FancyTree for JQuery) wants:

{title: "MyFirstObject", folder: true,},
{title: "SomeOtherItem", folder: true, children: [
   {title: "subItem1", folder: true},
   {title: "subItem2", folder: true},
   {title: "subItem3", folder: true}
]},
{title: "AnotherRandom", folder: true,}

How can I include the information for children, while still using a Model-based methodology to store my data?

Thanks!

(and thanks to everyone that have replied to any of my other posts - it's been extremely helpful in getting up to speed with .NET!)


Viewing all articles
Browse latest Browse all 9386

Trending Articles