如何建立与MVC4嵌套菜单 [英] How to build a nested menu with MVC4

查看:101
本文介绍了如何建立与MVC4嵌套菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进入一个自定义实现之前,我想问社区是否有在MVC4构建嵌套菜单出实体的任何内置的帮手。我有数据的层次结构是这样的:

 >文件夹1
>文件夹2
>>文件夹2.1
>>文件夹2.2
>>> 2.2.1项
>>> 2.2.1文件夹
>>文件夹2.3
>文件夹3

和我正在考虑通过嵌套数组的数组(嵌套数组[嵌套的数组,...])添加到控制器建设成相应的嵌套链接列表。之前,我潜入这个我有几个问题:


  1. 什么样的​​工具可用来解决这个问题,如果有的话?

  2. 您会用什么数据结构,如果你要构建自定义?

我完全新的MVC / C#所以任何建议/指针将是真棒。


解决方案

  

什么样的​​工具可用来解决这个问题,如果有的话?


我个人还没有碰到现成的解决方案来为这一点。


  

你会用什么数据结构,如果你要构建自定义?


最好的办法是建立一个自定义类此。像有节点字典
您可以如下建立一个自定义的:

 公共类树
{
    私人的TreeNode rootNode中;
    公共树节点的RootNode
    {
        {返回rootNode中; }
        组
        {
            如果(的RootNode!= NULL)
                Nodes.Remove(RootNode.Id);            Nodes.Add(value.Id,值);
            rootNode中=值;
        }
    }    公共字典节点{搞定;组; }    公共树()
    {
    }    公共无效BuildTree()
    {
        树节点父母;
        的foreach(在Nodes.Values​​ VAR节点)
        {
            如果(Nodes.TryGetValue(node.ParentId,出父)及和放大器;
                node.Id!= node.ParentId)
            {
                node.Parent =父母;
                parent.Children.Add(节点);
            }
        }
    }
}

如果您想了解更多详细信息,然后的此链接拥有所有你需要的。

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.

解决方案

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.

这篇关于如何建立与MVC4嵌套菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆