1

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:

  1. What kind of tools are available to help with this, if any?
  2. What data structures would you use if you had to build custom?

I'm totally new to MVC/C# so any suggestions/pointers would be awesome.

2
  • Is the data coming from datasource? Or it is static? Commented Mar 10, 2013 at 3:31
  • It's coming from a DTO, which I can build any way I wish from a POCO directly from the database. So datasource, not static (I think) Commented Mar 10, 2013 at 3:34

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow! Awesome. I didn't consider devoting a class to this. Thanks Bhushan

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.