Copied to clipboard

Flag this post as spam?

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


  • Pinal Bhatt 298 posts 390 karma points
    Mar 16, 2011 @ 20:45
    Pinal Bhatt
    0

    How to get the children count?

    Model.Children.Count or Model.Children.Count()  does not works.

    So how to get the number of chidren count?

    And specially how to get children count of specific type "NodeTypeAlias"?

     

  • Pinal Bhatt 298 posts 390 karma points
    Mar 16, 2011 @ 21:05
    Pinal Bhatt
    0

    Here is my workout for this. Can anybody from Umbraco HQ/Admin/MVP team verify if i am using right approch or there is some better way?

    @Model.ChildrenAsList.Count  -- gets the number of chidren count
    @Model.Descendants("nodeTypeAlias").Count(
    -- gets the number of chidren count of specific type.

  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Mar 17, 2011 @ 11:19
    Sebastiaan Janssen
    1

    You get ChildrenAsList if you've defined your Model as:

    @using umbraco.MacroEngines;
    @{ 
      var MyModel = (DynamicNode)Model;
    }
    @MyModel.ChildrenAsList.Count()

    However, by default your model is defined as a  "dynamic" instead of a "var", you can do .Children.Count()

    @using umbraco.MacroEngines;
    @Model.Children.Count()

    If you do .Descendants, you get the children but also grandchildren, great-grandchildren and so on. If you only want the direct children you can do something like

    @Model.Children.Where("NodeTypeAlias == \"someAlias\"").Count()

    Or, if you define MyModel as in the first sample:

    @MyModel.ChildrenAsList.Where(x => x.NodeTypeAlias == "TagFolder").Count()

     

     

     

  • Pinal Bhatt 298 posts 390 karma points
    Mar 17, 2011 @ 11:41
    Pinal Bhatt
    0

    Sebastiaan - Thanks a lot. this was quit informative.

    For me ChildrenAsList is working without any casting. 

    And yes @Model.Children.Count()is working smoothly for me now... i don't know why I was getting errror initially.

    Also .Descendants(), was not right for me - it gets from the the children + grandchildren +  great-grandchildren + so on. 

    Thanks once again for sharing usage of Model.Children.Where() - this is very useful functionality.


  • Pinal Bhatt 298 posts 390 karma points
    Mar 22, 2011 @ 16:39
    Pinal Bhatt
    0

    Hi Sebastiaan - Will Where() work with .Descendants() ? 

    @articlesNode.Descendants.Where("NodeTypeAlias == \"ArticlesExt\"").Count(

    gives me 0 (zero)

    And 

    @foreach(var page in articlesNode.Descendants())

    {
      @page.NodeTypeAlias<br/>

    gives following output:

    PBDeskHomepage
    PBDeskHomepage
    PBDeskHomepage
    PBDeskHomepage
    PBDeskHomepage
    ArticlesDocType
    ArticlesExt
    ArticlesDocType
    ArticlesDocType
    ArticlesDocType
    ArticlesExt
    ArticlesExt
    ArticlesExt
    ArticlesExt
    ArticlesDocType


  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Mar 22, 2011 @ 17:07
    Sebastiaan Janssen
    0

    You've forgotten the braces in the first expression:

    @articlesNode.Descendants().Where("NodeTypeAlias == \"ArticlesExt\"").Count() 

    There's also a shortcut, just insert the NodeTypeAlias in the Ancestor or Descendants call: 

    @articlesNode.Descendants("ArticlesExt")
  • Pinal Bhatt 298 posts 390 karma points
    Mar 22, 2011 @ 17:11
    Pinal Bhatt
    0

    oops... my mistake....  thanks a lot.

  • Eric S 10 posts 30 karma points
    Apr 19, 2011 @ 18:45
    Eric S
    0

    Was going crazy with this one. Almost went back to XSLT when all I was missing was the parenthesis after Count. Got confused with ChildrenAsList.Count working without them. 

Please Sign in or register to post replies

Write your reply to:

Draft