Reputation: 12915
Before I go into a custom implementation, I'd like to ask the community if there is any built-in helper for constructing a nested menu out of entities in MVC4. I have a hierarchy of data like this:
> Folder 1
> Folder 2
>> Folder 2.1
>> Folder 2.2
>>> Item 2.2.1
>>> Folder 2.2.1
>> Folder 2.3
> Folder 3
And I'm thinking about passing an array of nested arrays (of nested arrays [of nested arrays...]) into the controller to build into a list of corresponding nested links. Before I dive into this I have a couple questions:
I'm totally new to MVC/C# so any suggestions/pointers would be awesome.
Upvotes: 1
Views: 804
Reputation: 9448
What kind of tools are available to help with this, if any?
I personally haven't come across ready-made solution for this.
What data structures would you use if you had to build custom?
Best bet is to build a custom class for this. Like a tree
that has a dictionary of nodes
.
You can build a custom one as below:
public class Tree
{
private TreeNode rootNode;
public TreeNode RootNode
{
get { return rootNode; }
set
{
if (RootNode != null)
Nodes.Remove(RootNode.Id);
Nodes.Add(value.Id, value);
rootNode = value;
}
}
public Dictionary Nodes { get; set; }
public Tree()
{
}
public void BuildTree()
{
TreeNode parent;
foreach (var node in Nodes.Values)
{
if (Nodes.TryGetValue(node.ParentId, out parent) &&
node.Id != node.ParentId)
{
node.Parent = parent;
parent.Children.Add(node);
}
}
}
}
If you want more details, then this link has all you need.
Upvotes: 1