Domanda

I have a breadcrumb that loads its basic nodes from the DB (thx to nightowl for the help!). To archieve this functionality i implemented a 'CustomSiteMapNodeProvider' that implements 'ISiteMapNodeProvider'. The breadcrumb now works for the nodes that come from the DB. Now i wanted to add dynamic nodes as children of some nodes that came from the DB. My first impulse was to use the MvcSiteMapNodeAttribute.

[MvcSiteMapNodeAttribute(Title = "Execute", Area = "MyArea", DynamicNodeProvider = "My.Namespace.MyDynamicNodeProvider, My.Assembly", Clickable = false, ParentKey="KeyOfParentDbNode")]
public ActionResult Execute(Guid id, MyViewModel vm){
      return View(vm)
}

But this didnt work, the Breakpoint inside 'MyDynamicNodeProvider' got hit at startup, but the breadcrumb isnt shown when i call the 'Execute' method.

Here is how my DynamicNodeProvider looks like:

public class ProfileDynamicNodeProvider
    : DynamicNodeProviderBase
    {
        public override IEnumerable<DynamicNode> GetDynamicNodeCollection(ISiteMapNode node)
        {
            ProfileDao dao = DependencyResolver.Current.GetService<ProfileDao>();

            // Create a node for each element 
            foreach (Profile profile in dao.GetAll())
            {
                DynamicNode dynamicNode = new DynamicNode
                {
                    Title = profile.Name,
                    //Key = "Profile_"+profile.IdProfile,
                    ParentKey = "KeyOfParentNode",
                };
                dynamicNode.RouteValues.Add("id", profile.IdProfile);

                yield return dynamicNode;
            }
        }

    }

What can i do to make this work?

edit: could it be, that they are processed in the wrong order? i reach the breakpoint of my DynamicNodeProvider before i reach the breakpoint of the CustomSiteMapNodeProvider.

È stato utile?

Soluzione

The node the DynamicNode is declared on is just a template node. That node will not be added to the SiteMap. Furthermore, any properties that you set on the DynamicNode objects within your provider will overwrite what you have defined on the template node.

In your example, the "KeyOfParentDbNode" value will be overwritten by the value "KeyOfParentNode" in every case, which could be the source of your problem. Also, be sure there is a node somewhere that sets the key property explicitly to "KeyOfParentNode" (that is, if "KeyOfParentNode" is what you intended to set).

In addition, the Clickable property will be set to false for all of your DynamicNodes.

If you are using 4.3.0 or higher, the order doesn't matter because the SiteMapNodeProviders instantiate all of the nodes before the SiteMapBuilder adds them to the SiteMap so all of the parent nodes are all available at that point in time.

It is often helpful to use a @Html.MvcSiteMap().SiteMap() HTML helper to view the nodes to see if they are nested correctly. You could also check by calling MvcSiteMapProvider.SiteMaps.Current.FindSiteMapNodeFromKey("TheDynamicNodeKey") to see if the controller and action (and other) properties are being set correctly (although in your example it should be fine).

Another issue you might be having is that MvcSiteMapProvider uses strings when doing the comparison and you are setting the "id" to a Guid.

// You have
dynamicNode.RouteValues.Add("id", profile.IdProfile);

// Should be
dynamicNode.RouteValues.Add("id", profile.IdProfile.ToString());
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top