2012-05-15 131 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