add node at runtime

Apr 16, 2010 at 9:46 AM

How do I add a node at runtime? The below code runs fine, but seems not to work. I can step through and see that the node is not added to teh parent collection:

MvcSiteMapNode nd = new MvcSiteMapNode(SiteMap.Provider, "abc_xyz_123", explicitResourceKeys);

nd.Controller = "ApplicationForum";
nd.Action = "Alert";
nd.Title = "Test Dynamic Nodes";
nd.Url = "http://www.codeplex.com";
nd.Visibility = MvcSiteMapNodeVisibility.Full;
           
 SiteMap.CurrentNode.ParentNode.ChildNodes.Add(nd);

I have tried variations on this to no avail, and I can see simialr questions in the forum go unanswered on this topic.

Just to add that I do like this solution, but it is starting to seem like it only works well in simple cases? Thats probbaly more a reflection of the lack of documentation and examples no doubt...or my inability to find them!

Any assistance will be very much appreciated.

 

Coordinator
Apr 16, 2010 at 10:39 AM

Do you have caching enabled?

Apr 17, 2010 at 3:00 AM

 cacheDuration="0"

To note that I do not seeit in debug mode either. ParentNode.ChildNodes.Count reflects that it was never added...

By your response, I at least know that this should work, right? 

Ok - will plod on and see how I go. May need to abandon the approach altogther though, rapidly exceeding the benefit here...

Apr 17, 2010 at 4:06 AM

I seem to have some fundamental issue here. The following, which shoud set all Title's to "xxxx" has no effect whatsoever:

foreach (MvcSiteMapNode node in SiteMap.CurrentNode.ParentNode.ChildNodes)
 {
       node.Title = "xxxx";
 }

The nodes in question simply retain their original title.

All indications are that it works, but is somehow later removed?? Perhaps I am not appreciating when things are occuring, though my understanding is that at the point I call this code (in an action method on a controller) that the sitemap has long since been initialised. And I know for sure my cahce setting is not the culprit (unless a cache bug is the underlying issue).

 

 

 

 

Apr 17, 2010 at 4:10 AM

ok - so now I am thoerising that somehow the incorrect provider is being used. My code is definitely working, but its having no effect on my in memory mvc site map. I will poke around this avenue of thought for a bit, but if you can advise why / how this could be the case??

Apr 17, 2010 at 5:26 AM

not a provider issue. Here's where I am at:

CURRENT CONFIG

  <add name="MvcSiteMap"
             type="MvcSiteMap.Core.MvcSiteMapProvider"
             siteMapFile="~/Web.Sitemap"
             securityTrimmingEnabled="true"
             enableLocalization="true"
             cacheDuration="10"
             scanAssembliesForSiteMapNodes="true"
             skipAssemblyScanOn="MvcSiteMap.Core"
             treatAttributesAsRouteValues="true"
             defaultControllerName="Home"
             defaultActionName="Index"
             aclModule="MvcSiteMap.Core.DefaultMvcSiteMapAclModule, MvcSiteMap.Core"/>

CURRENT ACTION METHOD

public ActionResult Forum(int id)
        {
            NameValueCollection explicitResourceKeys = new NameValueCollection();

            MvcSiteMapNode nd = new MvcSiteMapNode(SiteMap.Provider, "abc_xyz_123", explicitResourceKeys);
            nd.Controller = "ApplicationForum";
            nd.Action = "Alert";
            nd.Title = "Test Dynamic Nodes";
            nd.Url = "http://www.codeplex.com";
            nd.Visibility = MvcSiteMapNodeVisibility.Full;

            // NOT WORKING!
            SiteMap.CurrentNode.ParentNode.ChildNodes.Add(nd);

          // WORKING - ALL EXISITNG NODE UPDATE ACCORDINGLY.            
          SiteMap.CurrentNode.Title = "xxxx123";

            foreach (MvcSiteMapNode node in SiteMap.CurrentNode.ParentNode.ChildNodes)
            {
                node.Title = "xxxx";
            }
 
            ApplicationForumViewModel model = new ApplicationForumViewModel(repository, id);
            model.ContentRight.Add("VotePanel");
            ViewData["tabindex"] = 0;
            ViewData["id"] = id;
            return ToView(model);
        }

CURRENT RESULTS

Will modify nodes (as in, the Title is now being set as expected)

Will not add any new nodes, as per original issue / topic under discussion.

Variation of the cache value simply stop node updates from working (cache='0' will prevent node updates), but never assist with teh original issue of being abel to add nodes.

 

 

 

 

 

May 1, 2010 at 7:40 AM

I never did get an answer to this, neither did several other posters in these forums struggling with the same thing. A post was just made explaining the problem and showing a simple work around:

http://mvcsitemap.codeplex.com/Thread/View.aspx?ThreadId=207882

I can now add nodes at runtime without issue.