-2

这是我的数据库表

数据库表

如何生成站点地图?

控制器是类别

动作是 Id

就是这样


TreeMenu.cs:

using System;
using System.Collections.Generic;

namespace Pmvc.Models
{
    public partial class TreeMenu
    {
        public int TreeId { get; set; }
        public int TreeParentId { get; set; }
        public string TreeName { get; set; }
        public List<TreeMenu> Children { get; set; }
    }

}

StoreDetailsDynamicNodeProvider.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcSiteMapProvider.Extensibility;
using Pmvc.Models;


namespace Pmvc
{
    public class StoreDetailsDynamicNodeProvider : DynamicNodeProviderBase
    {
        PMVCEntities pmvcDB = new PMVCEntities();
        public override IEnumerable<DynamicNode> GetDynamicNodeCollection()
        {

            var nodes = new List<DynamicNode>();

            foreach (var treemenu in pmvcDB.TreeMenu)
            {
                nodes.Add(CreateNode(treemenu));
            }

            return nodes;
        }

        private DynamicNode CreateNode(TreeMenu treemenu)
        {

            DynamicNode node = new DynamicNode();
            node.Action = "ProductDetails";
            node.Controller = "Product";
            node.Title = treemenu.TreeName;

            if (treemenu.Children != null)
            {
                foreach (var child in treemenu.Children)
                {
                    node.Children.Add(CreateNode(child)); //This line is wrong!
                }
            }
            return node;

        }

    }


}

错误的:

'MvcSiteMapProvider.Extensibility.DynamicNode' 不包含 'Children' 定义,并且找不到扩展方法 'Children'


public class StoreDetailsDynamicNodeProvider : DynamicNodeProviderBase 
    {
        PMVCEntities pmvcDB = new PMVCEntities();
        public override IEnumerable<DynamicNode> GetDynamicNodeCollection()
        {
            // Build value 
            var returnValue = new List<DynamicNode>();

            // Create a node for each album 
            foreach (var treemenu in pmvcDB.TreeMenu)
            {
                DynamicNode node = new DynamicNode();
                node.Action = "ProductDetails";
                node.Controller = "Product";
                node.Title = treemenu.TreeName;
                node.RouteValues.Add("id", treemenu.TreeId);
                returnValue.Add(node);
            }

            // Return 
            return returnValue;
        } 
    }

这是我的桌子

TreeId TreeParentId 树名

1 0 类别 1

2 1 菜单项 X

3 1 菜单项 Y

4 1 菜单项 Z

5 0 类别 2

6 5 其他菜单 1

7 5 其他菜单 2

8 0 空类别

9 7 菜单 Lvl 3

如何编写子节点代码?

4

1 回答 1

1

查看MvcSitemap 提供程序。编写从数据库读取的动态提供程序非常容易。

编辑 - 您是否阅读过文档或尝试实施任何内容?从他们的网站上几乎逐字复制:

在站点地图配置文件中,在适当的位置添加动态节点:

<mvcSiteMapNode 
    title="Details" 
    action="Details"   
    dynamicNodeProvider="MyProjectName.MyDynamicNodeProvider, MyProjectName" />

然后,编写实际的节点提供程序:

public class MyDynamicNodeProvider
    : DynamicNodeProviderBase 
{
    public override IEnumerable<DynamicNode> GetDynamicNodeCollection() 
    {
        // Build value 
        var returnValue = new List<DynamicNode>();

        // ... Here, get values from your database and build up
        // the list of nodes.  They can be in a tree structure
        // too since it appears that's how your data is - just
        // build the nodes in that way with sub-lists.

        // Return 
        return returnValue; 
    } 
}

就是这样。

编辑 2 - 解决其他答案。

您提供的代码使树结构变平。在您的原始帖子中,您表明您已经能够打印树结构。无论您看到什么代码,都可以应用深度优先遍历的相同原则。

以防万一这是一个模型而不是实际代码,这里有一些快速的伪代码来演示深度优先遍历。

public override IEnumerable<DynamicNode> GetDynamicNodeCollection() 
{
    // Build value 
    var nodes = new List<DynamicNode>();

    // Get all categories without parents.
    var rootCategories = db.GetRootCategories();  

    // Loop all root level categories, creating a node for each and adding
    // to the return value.
    foreach (var category in rootCategories)
    {
        nodes.Add(CreateNode(category));
    }
    return nodes;
}

private DynamicNode CreateNode(Category category)
{
    // Create a new node for the category
    DynamicNode node = new DynamicNode();

    node.Name = category.Name;
    // ... set node properties here ...

    // If the category has children, then continue down the tree
    // and create nodes for them.  This will continue recursively
    // to the bottom of the tree.
    // Note that I'm just guessing on the property names because
    // you didn't show us the code for what your entity actually
    // looks like.  This is why you should always show what code
    // you've attempted - you can get better, more precise answers
    // instead of complete guesses.
    if (category.Children != null)
    {
        foreach (var child in category.Children)
        {
            // For each child, recursively branch into them.
            node.Children.Add(CreateNode(child));
        }
    }

    return Node;
}

请注意,这没有经过测试或研究 - 它只是为了显示树遍历。

于 2012-05-15T14:36:35.133 回答