Developer forum

Forum » CMS - Standard features » Render navigation in Page template

Render navigation in Page template

Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi guys,

I have been struggling with something apparently elementary, but I cannot figure out what I am doing wrong.

The scope is to render a navigation directly in the Page template.

I am trying to use 

Navigation.RenderNavigation(navigationTemplate, navigationSettings)

With a regular navigation template.

I have correctly defined the NavigationSettings:
 

var navigationSettings = new NavigationSettings();
    navigationSettings.StartLevel = 1;
    navigationSettings.StopLevel = 10;
    navigationSettings.ExpandMode = ExpandMode.All;
    navigationSettings.RootAreaId = 9;
    navigationSettings.RootPageId = 2448;

The navigation template inherits  Dynamicweb.Rendering.ViewModelTemplate<Dynamicweb.Frontend.Navigation.NavigationTreeViewModel>

No matter what I do, I get this error (which is useless):

 

Error executing template "Files/Templates/Designs/Hive/Navigation/TabsNavigation.cshtml"
System.NullReferenceException: Object reference not set to an instance of an object.
   at Dynamicweb.Rendering.Template.RenderRazorTemplate()
Line: 1

I am clearly missing something but I cannot figure out what.

Thank you in advance for any suggestions.

Adrian


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

The error you're encountering, a System.NullReferenceException, typically indicates that some component or property required by the Navigation.RenderNavigation function is not initialized or set up correctly. Based on your provided code, here are some areas to investigate and resolve the issue:

Key Points to Check:

  1. NavigationSettings Configuration:

    • Ensure all mandatory properties in NavigationSettings are correctly set. For example:
      • StartLevel and StopLevel should match your site’s navigation hierarchy.
      • If RootAreaId or RootPageId is used, ensure they are valid IDs within your setup.
      • If RootNavigationTag is preferred, use it consistently over RootAreaId/RootPageId.
  2. Navigation Template (TabsNavigation.cshtml):

    • The template must inherit the correct ViewModelTemplate. From your description, you have defined:
      @inherits Dynamicweb.Rendering.ViewModelTemplate<Dynamicweb.Frontend.Navigation.NavigationTreeViewModel>
      
      • Verify that the template matches the expected data model of the navigation being rendered.
      • Ensure no null references are accessed inside the template. For instance, ensure Model.Nodes or Model.Root are not null before iterating over them
  3. Rendering Context:

    • Check that the Navigation.RenderNavigation call is executed in the correct rendering context (e.g., a Razor page or template) where the required dependencies and data are accessible.
  4. Null Checks:

    • Validate that the NavigationTreeViewModel returned by RenderNavigation contains the expected data before accessing it in the template.
    • Example validation in Razor:
      @if (Model?.Nodes != null && Model.Nodes.Any())
      {
          <ul>
          @foreach (var node in Model.Nodes)
          {
              <li>@node.Name</li>
          }
          </ul>
      }
      
  5. Debugging the Error:

    • Add logging or debugging within the template to check the state of Model.Nodes or Model.Root.
    • Test with simpler navigation templates to isolate the issue.
  6. ExpandMode Configuration:

    • ExpandMode determines which nodes are included in the navigation. Ensure that the mode set (ExpandMode.All) aligns with your expectations. For example:
      • ExpandMode.All includes all nodes.
      • ExpandMode.Path includes only nodes along the active path

Example Adjusted Code:

Ensure your NavigationSettings and template logic are robust against null references:

var navigationSettings = new NavigationSettings
{
    StartLevel = 1,
    StopLevel = 10,
    ExpandMode = ExpandMode.All,
    RootAreaId = 9, // Ensure this is valid
    RootPageId = 2448 // Ensure this is valid
};

Template Check:

@inherits Dynamicweb.Rendering.ViewModelTemplate<Dynamicweb.Frontend.Navigation.NavigationTreeViewModel>

@if (Model?.Nodes != null)
{
    <ul>
    @foreach (var node in Model.Nodes)
    {
        <li>
            <a href="@node.Link">@node.Name</a>
            @if (node.Nodes != null)
            {
                <ul>
                @foreach (var subNode in node.Nodes)
                {
                    <li>
                        <a href="@subNode.Link">@subNode.Name</a>
                    </li>
                }
                </ul>
            }
        </li>
    }
    </ul>
}

By addressing these areas, you should be able to resolve the issue and render the navigation correctly. If the issue persists, verify the validity of RootAreaId and RootPageId against your site’s configuration.

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

In doubt, my answer was written by AI :-)

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

You are using both rootareaid and rootpageid - that can be an issue if the pageid does not reside on a website with the id you have in rootareaid:

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Nicolai,

Thank you for the feedback. The AI seems to be doing a very good job!

All checks mentioned were performed. In fact, I have tried using an empty template just to be sure that I am not generating any errors in the template.

The PageID I am using is in the correct AreaID (I have also tried using only pageId and got the same result).

My guess would be the rendering context.

I have a page template inheriting  @inherits Dynamicweb.Rendering.ViewModelTemplate<Dynamicweb.Frontend.PageViewModel>

In this page I am using an include to get the file where I define the NavigationSettings and access the Navigation.RenderNavigation method. This file does not have any inheritance. I have tried a few but it only got worse.

The template is inheriting  Dynamicweb.Rendering.ViewModelTemplate<Dynamicweb.Frontend.Navigation.NavigationTreeViewModel>

Can I test somehow if the NavigationContext is correct?

Thank you,
Adrian

 

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

From the exception, you are missing a null check in 

TabsNavigation.cshtml

Can you share that template?

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

And your exact code using to make this call:

Navigation.RenderNavigation(navigationTemplate, navigationSettings)

Including the instantiation of the variables....

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

I have attached a zip file.

Thank you,
Adrian
 

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Hi Adrian

I expect this line to be a problem:

string tabPageId = node.node.PageId;

As a node is a NavigationTreeNodeViewModel and that type does not have a node property.

Do you have intellisense in your project?

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Nicolai,

I have removed the entire code of the template and left only this piece:
 

@{
    var nodes = Model.Nodes
} 

I have also tried with an empty template and I got the same result.

I have tried passing an empty string to the Template property of RenderNavigation and the error disappeared.

It has something to do with transforming the template. Maybe the path to the template is wrong?

Adrian

 

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Well, changing the path from absolute to relative did the trick :)

this works:
string navigationTemplate = "../Navigation/TabsNavigation.cshtml";

This does not work:

string navigationTemplate = "/Files/Templates/Designs/Hive/Navigation/TabsNavigation.cshtml";

I have other places where the second option works. Weird.

Adrian 

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Hi Adrian

Yes, that sounds weird if that works. We always prepend ./Files/Templates/.to the passed template file:

Either way - the very latest versions of both 9+10 will return "Template not found" message when you pass in a template path that does not exist.

 

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Nicolai,

I agree. I have always used the full path for template files. Maybe it's an issue just with this version? I am using 9.15.7.

It is always good to know that we will get a cleaner error message.

Thank you,

Adrian

 

You must be logged in to post in the forum