Copied to clipboard

Flag this post as spam?

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


  • Dibs 202 posts 991 karma points
    Jan 23, 2017 @ 15:58
    Dibs
    0

    partial view return children

    Dear Umbraco Team

    I would like to return children from a level 2 node, the following code returns only the level 2 child. The code is a partial view,any assistance would be great ?

     @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
      @{
           var root = Model.Content.AncestorOrSelf(1);
           int docId = Model.Content.Id;
           var nodes = root.Children.Where(x => x.Level == 2 && x.Name == "Departments");
       }
     @{
     foreach(var node in nodes){
          <h3><a href="@node.Url">@node.Name</a></h3>      
          <p>@(Umbraco.Truncate(node.GetPropertyValue("mainContent").ToString(), 200, true))</p> 
         }       
    

    }

  • Nik 1625 posts 7295 karma points MVP 7x c-trib
    Jan 23, 2017 @ 16:19
    Nik
    0

    Hi Dibs,

    Taking your code from above, I'd added some comments about what the x.Level code segment is doing. I've also removed the x.Name == "Departments" as you are effectively filtering for a single node by using the "Name" to filter against in that manner.

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
       var root = Model.Content.AncestorOrSelf(1);
       int docId = Model.Content.Id;
       //The x.Level == 2 means only get items where their level is equal to 2
       //If you put x.Level <= 4 it would get everything upto level 4 for example
       var nodes = root.Children.Where(x => x.Level == 2);
    }
    @{
        foreach(var node in nodes){
           <h3><a href="@node.Url">@node.Name</a></h3>      
           <p>@(Umbraco.Truncate(node.GetPropertyValue("mainContent").ToString(), 200, true))</p> 
        }       
    }
    

    Saying that, however, if you are trying to list all of children of the "Departments" node I would do the following, working on the assumption that the current page is NOT the departments page.

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
       var root = Model.Content.Site();
    
       ///Personally I would rather be using a document type alias filter here as this has limited reusability, using the First Linq expression to get the first occurrence of the Departments node as I don't believe it should be possible to have multiple. 
       var departmentNode = root.Children.First(x => x.Name == "Departments");
    }
    @{
        foreach(var node in departmentNode.Children){
           <h3><a href="@node.Url">@node.Name</a></h3>      
           <p>@(Umbraco.Truncate(node.GetPropertyValue("mainContent").ToString(), 200, true))</p> 
        }       
    }
    
  • Dibs 202 posts 991 karma points
    Jan 23, 2017 @ 16:29
    Dibs
    0

    Cheers Nik

    i did get what i wanted by adding a nested foreach loop like so:

     @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
     @{
           var root = Model.Content.AncestorOrSelf(1);
           int docId = Model.Content.Id;
           var nodes = root.Children.Where(x => x.Level == 2 && x.Name == "Departments");
       }
     @{
     foreach(var node in nodes){
     foreach(var childnode in node.Children)
          <h3><a href="@childnode.Url">@childnode.Name</a></h3>          <p>@(Umbraco.Truncate(childnode.GetPropertyValue("mainContent").ToString(), 200, true))</p> 
        }
     }      
    

    But taking on your point how i have coded it, would your way still be better ?

    Thanks Dibs

  • Nik 1625 posts 7295 karma points MVP 7x c-trib
    Jan 23, 2017 @ 17:13
    Nik
    100

    Hi Dibs,

    No problem, to be fair it actually depends on exactly what you are trying to achieve.

    Let's say for example you have a the following document types:

    Departments Repository (alias of departmentRepository)

    Department (alias of department)

    Where Department(s) can only be created under a DepartmentsRepository node.

    Then to make the partial view re-usable I would structure it like this:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        var repositoryNode = Model.Content.AncestorOrSelf("departmentRepository");
        //Make sure we have a repository to work with
        if(repositoryNode != null){
            foreach(var department in repositoryNode.Children.Where(x=>x.DocumentTypeAlias.Equals("department")))
            {
                <h3><a href="@childnode.Url">@childnode.Name</a></h3>
                <p>@(Umbraco.Truncate(childnode.GetPropertyValue("mainContent").ToString(), 200, true))</p>
            }
        }
    }
    

    This was you can re-use your partial view on any page that is underneath a departmentRepository and it will render out the entire list.

    As I mentioned at the start, it really depends on use-case though, the more you can make things flexible and reusable, often the easier it can be :-)

    Nik

  • Dibs 202 posts 991 karma points
    Jan 25, 2017 @ 14:54
    Dibs
    1

    Cheers Nik : )

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies