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.
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
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.
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"?
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.
You get ChildrenAsList if you've defined your Model as:
However, by default your model is defined as a "dynamic" instead of a "var", you can do .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
Or, if you define MyModel as in the first sample:
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.
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
You've forgotten the braces in the first expression:
There's also a shortcut, just insert the NodeTypeAlias in the Ancestor or Descendants call:
oops... my mistake.... thanks a lot.
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.
is working on a reply...