DynamicNodeProvider not working right for parent and children

Sep 14, 2011 at 2:30 PM

Hello,

Before I ask my question, I'd like to thank you for taking the time to build this plugin which is really useful to many of us. Thanks to it, and once I fix this problem regarding the question I'm going to ask, it's going to be a treat on my website.

In my website I have a node which will have children and grandchildren nodes. For that, I coded a different DynamicNodeProvider for each situation as I found other people doing when I searched the other discussions here.

So my website structure is as follows:

Home => ApplicationModules (children of home) => ModuleSections (children of ApplicationModules)

ApplicationModules and ModuleSections are both dynamic nodes. The following is the sitemap I have coded for them:

<?xml version="1.0" encoding="utf-8" ?>
<mvcSiteMap xmlns="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-3.0" enableLocalization="true">
  <!-- Aplica��o -->
  <mvcSiteMapNode title="Aplica��es" controller="Home" action="Index" key="parentApplications" changeFrequency="Always" updatePriority="Normal">
<mvcSiteMapNode title="Criar Aplica��o" controller="Home" action="Create"/>
    <mvcSiteMapNode title="Editar Aplica��o" controller="Home" action="Edit"/>    
     <!--M�dulos--> 
    <mvcSiteMapNode title="M�dulos" action="Index" controller="ApplicationModule" dynamicNodeProvider="Publico.IpadHD2Portal.AMIndexDynamicNodeProvider, Publico.IpadHD2Portal">
      <mvcSiteMapNode title="Criar M�dulo"  action="Create" controller="ApplicationModule" url="~/ApplicationModule/Create"/>
      <mvcSiteMapNode title="Editar M�dulo"  action="Edit" controller="ApplicationModule" dynamicNodeProvider="Publico.IpadHD2Portal.AMEditDynamicNodeProvider, Publico.IpadHD2Portal"/>
      <mvcSiteMapNode title="Ordenar M�dulos" controller="ApplicationModule" action="SortModules" dynamicNodeProvider="Publico.IpadHD2Portal.AMSortDynamicNodeProvider, Publico.IpadHD2Portal"/>
       <!--Sec��es--> 
      <mvcSiteMapNode title="Sec��es"  action="Index" controller="ModuleSection" dynamicNodeProvider="Publico.IpadHD2Portal.MSIndexDynamicNodeProvider, Publico.IpadHD2Portal">
        <mvcSiteMapNode title="Criar Sec��o"  action="Create" controller="ModuleSection" url="~/ModuleSection/Create"/>
        <mvcSiteMapNode title="Editar Sec��o"  action="Edit" controller="ModuleSection" dynamicNodeProvider="Publico.IpadHD2Portal.MSEditDynamicNodeProvider, Publico.IpadHD2Portal"/>
        <mvcSiteMapNode title="Ordenar Sec��es"  action="SortSections" controller="ModuleSection" dynamicNodeProvider="Publico.IpadHD2Portal.MSSortDynamicNodeProvider, Publico.IpadHD2Portal"/>
        <mvcSiteMapNode title="Associar Publicidade"  action="SectionAds" controller="ModuleSection"  dynamicNodeProvider="Publico.IpadHD2Portal.MSAdsDynamicNodeProvider, Publico.IpadHD2Portal"/>
      </mvcSiteMapNode>
    </mvcSiteMapNode>
  </mvcSiteMapNode> 
</mvcSiteMap>

(Sorry about the special characters which aren't showing up)

And here go the dynamicNodeProviders:
AMIndexDynamicNodeProvider:
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports MvcSiteMapProvider.Extensibility
Imports Publico.IpadHD2Portal.Models
Imports System.Web.Caching

Public Class AMIndexDynamicNodeProvider
    Inherits DynamicNodeProviderBase

    Public Overrides Function GetDynamicNodeCollection() As System.Collections.Generic.IEnumerable(Of MvcSiteMapProvider.Extensibility.DynamicNode)
        Dim nodes As New List(Of DynamicNode)
        Dim node As New DynamicNode
        Dim ctx As New IpadHD2DataContext

        Try
            Dim applications As List(Of Application) = ctx.Applications.ToList()

            If (applications IsNot Nothing And applications.Count > 0) Then
                For Each app In applications
                    node = New DynamicNode
                    node.Title = "M�dulos de " & app.name
                    node.Controller = "ApplicationModule"
                    node.Action = "Index"
                    node.RouteValues.Add("ApplicationId", app.id.ToString())
                    node.Key = "app_" & app.id.ToString()
                    node.ParentKey = "parentApplications"

                    nodes.Add(node)
                Next
            End If
        Catch ex As Exception
        End Try
        Return nodes
    End Function
End Class

MSIndexDynamicNodeProvider:
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports MvcSiteMapProvider.Extensibility
Imports Publico.IpadHD2Portal.Models
Imports System.Web.Caching


Public Class MSIndexDynamicNodeProvider
    Inherits DynamicNodeProviderBase


    Public Overrides Function GetDynamicNodeCollection() As System.Collections.Generic.IEnumerable(Of MvcSiteMapProvider.Extensibility.DynamicNode)
        Dim nodes As New List(Of DynamicNode)
        Dim node As New DynamicNode
        Dim ctx As New IpadHD2DataContext

        Try
            Dim modules As List(Of [Module]) = ctx.Modules.ToList()
            Dim sections As List(Of Section) = ctx.Sections.ToList()

            If (sections IsNot Nothing And sections.Count > 0) Then
                If (modules IsNot Nothing And modules.Count > 0) Then
                    For Each sect In sections
                        node = New DynamicNode
                        node.Title = "Sec��es de " & modules.Where(Function(t) t.id = sect.module_id).FirstOrDefault().name
                        node.Controller = "ModuleSection"
                        node.Action = "Index"
                        node.RouteValues.Add("id", sect.module_id.ToString())
                        node.ParentKey = "app_" & modules.Where(Function(t) t.id = sect.module_id).FirstOrDefault().application_id.ToString()

                        nodes.Add(node)
                    Next
                End If
            End If
        Catch ex As Exception
        End Try
        Return nodes
    End Function
End Class


Here goes the problem:

 

When I enter the Home Controller, I see the correct breadcrumb title that I defined in the sitemap. Once I enter the level of ApplicationModules, an incorrect ApplicationModule title is displayed in the breadcrumb. Depending on how I form the ParentKey and Key for this dynamicNodeProvider, the breadcrumb may even not show at all. When I enter the ModuleSection level, the correct ApplicationModule is displayed, but the link to Home is no longer in the breadcrumb. Inside the ModuleSection, there are 3 situations: If I have no ModuleSections (items from the db) no breadcrumb is created (which is right according to the dynamicNodeProvider I coded). If there is only 1 ModuleSection from the db, I don't get breadcrumbs too. But if I have 2 or more ModuleSections, the breadcrumb is displayed.

Please explain to me what's going on and how to use the ParentKey and Key attributes correctly.