2012-05-15 57 views
-2

这是我的数据库表如何生成网站地图?(asp.net MVC3)

DB Table

如何生成网站地图?

控制器

作用是标识

是像它

Page


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' 不包括 '儿童' 的定义,并没有发现扩展方法 '儿童'


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 Tr的eeName

1 0 1类

2 1菜单项目X

3 1菜单项目Y

4 1菜单项ž

5 0 2类

6 5其他菜单1

7 5其他菜单2

8 0空类别

9 7菜单拉特3

我怎样写子节点的代码?

+4

许多SO贡献者不喜欢为海报编写代码:它是一个编程问答网站,而不是外包网站......您是否尝试过某些东西?顺便说一句,你应该已经发布你的表格内容和预期的结果作为文本。 – Seki

回答

1

看一看的MvcSitemap provider。编写从您的数据库读取的dynamic provider是相当容易的。

编辑 - 你阅读文档,或试图实现什么?从他们的网站复制几乎一字不差:

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

<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 - 寻址其他答案。

您提供的代码将树结构展平。在您的原始文章中,您显示您已经能够print the tree structure。无论您在视图中使用哪种代码,都可以应用与深度优先遍历相同的原则。

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

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; 
} 

请注意,这是没有办法测试或研究 - 它只是显示树遍历。

+1

如何在父节点中添加子节点? 使用动态站点地图? – jgedean

+1

这是我的代码: – jgedean