Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Anthony Candaele 1197 posts 2049 karma points
    Apr 26, 2012 @ 10:29
    Anthony Candaele
    0

    how to get nodes by Document Type in Razor

    Hi,

    For a navigation menu I need to show all nodes of a certain document type ('member')

    The current Razor script I have render the navigation menu by showing all the child nodes of the parent 'members area' node:

    @using umbraco.MacroEngines
    @inherits umbraco.MacroEngines.DynamicNodeContext
                      
    @{
      List<stringcategories new List<string{"Professor""Researcher",  "Teaching Assistant""Practitioning Assistant""Affiliate Member"};
      DynamicNodeList categorieItems new DynamicNodeList();
      
      foreach (var category in categories)
          {
            categorieItems @Model.Children.Where("memberCategory == \"" category "\"");
            
            if (categorieItems.Items.Any())
            {
                <h3>@category</h3>
                <ul class="nav">
                    @foreach(DynamicNode item in categorieItems)
                    {
                        <li><href="@item.Url">@item.Name</a></li>
                    }
                </ul>
            }
          }
      }

    As you can see in the screenshot, this works fine if you are on the 'Members' page:

    However, when you select a single member, the navigation disappears:

    So I wonder if it is possible to adjust the Razor script, so that it doesn't traverse on Children of the 'Members Area' node, but renders all nodes of the document type 'member' so that the navigation menu also shows the other members when a sinlge member is selected in the navigation.

    Thanks for your help,
    Anthony

  • gilad 185 posts 425 karma points
    Apr 26, 2012 @ 11:18
    gilad
    2

    Hi Anthony.

    You can use AncestorOrSelf("Document type alias") to get the closest parent or self from this type and then get the children.

    In your example :

    categorieItems @Model.AncestorOrSelf("members Folder Document Type Alias").Children.Where("memberCategory == \"" category "\"");

    Or you can get the parent before the loop like this :

    DynamicNode parent = @Model.AncestorOrSelf("members Folder Document Type Alias");

    and then inside the foreach :

     

     

    categorieItems = parent.Children.Where("memberCategory == \"" category "\"");

     

    Cheers.

     

  • Anthony Candaele 1197 posts 2049 karma points
    Apr 26, 2012 @ 11:49
    Anthony Candaele
    0

    Hi gilad,

    Again you come to the rescue, thanks. I used the first method:

    categorieItems @Model.AncestorOrSelf("members Folder Document Type Alias").Children.Where("memberCategory == \"" category "\"");

    And this works fine

    Greetings,
    Anthony

Please Sign in or register to post replies

Write your reply to:

Draft