.Net版本 递归生成多级菜单

public class TreeViewDataModel {
public List node { get; set; }
public int id { get; set; }
public string nodeName { get; set; }
public int PId{ get; set; }
public TreeViewDataModel() {
this.node = new List();
this.PId= 0;
}

    //add  data
    public List AddTreeData()
    {
        List list = new List() {
        new TreeViewDataModel(){id=1,nodeName="根节点",PId=0},
        new TreeViewDataModel(){id=2,nodeName="我是第一级菜单",PId=1},
        new TreeViewDataModel(){id=3,nodeName="我是第二级菜单",PId=2},
        new TreeViewDataModel(){id=4,nodeName="我是第三级菜单",PId=3},
        new TreeViewDataModel(){id=5,nodeName="我是第四级菜单",PId=4},
        new TreeViewDataModel(){id=6,nodeName="百度",ParentId=5},
        new TreeViewDataModel(){id=6,nodeName="谷歌",ParentId=5},
        new TreeViewDataModel(){id=6,nodeName="酷狗",ParentId=5},
        };
        return list;
    }

    //递归生成树
    public List GetTree(int printId, List node) {
        List mainNodes = node.Where(x => x.PId== printId).ToList();
        List otherNodes = node.Where(x => x.PId!= printId).ToList();
        foreach (TreeViewDataModel dpt in mainNodes)
        {
            dpt.node = GetTree(dpt.id, otherNodes);
        }
        return mainNodes;
    }

}

你可能感兴趣的:(C#,C#,.net,ASP.NET,MVC)