I recently downloaded Umbraco 7plus the starter kit. I then focused my attention on the MainNavigation.cshtml page. Since I wanted intellisense in my razor view, I changed out this code @{ var home = CurrentPage.Site(); } @if (home.Children.Any())
to
@if (Model.Content.Children.Any())
But when the code gets to this line @childPages(childPage.Children) and calls the helper method @helper childPages(dynamic pages), I'm getting an error, pages.Any() is null.
Question: How do I convert the pages.Any() to get the collection count. Note: Pages ResultsView shows three items (The Starter Kit, Basics, MasterClasses").
Thanks
here is the code
@if (Model.Content.Children.Any())
{
@* Get the first page in the children *@
var naviLevel = Model.Content.Children.First().Level;
@* Add in level for a CSS hook *@
<ul class="level-@naviLevel">
@* For each child page under the home node *@
@foreach (var childPage in Model.Content.Children)
Count does not work on the pages object (childPage.Children) being passed in as Dynamic.
Yes, when I query Model.Content.Children.Count it returns 5, which is wrong. Since the parent object is Learn , there are only 3 children (The Starter Kit, Basics, MasterClasses").
When I break into the code, The non public member (Results View) shows 3. I just don't know the systax to write to iterate through.
Note: the object "pages" is coming in as System.Linq.OrderedEnumerable<Umbraco.Core.Models.IPublishedContent,int>}
Do you know I could cast pages or convert to so I can iterate through?
rather than if (pages.Any()) can you not use if (pages != null && pages.Any())? I'm assuming that the problem is due to pages being null which causes .Any() to throw an exception which woiuld be the same even if you change it to use Count().
That didn't work either and I know pages.Any() is valid as when I break into the code, I see 3 values in the ResultsView.
I think if you could help me convert my pages object (System.Linq.OrderedEnumerable<Umbraco.Core.Models.IPublishedContent,int>} to something??? that's of the right type, then perhpas I could iterate through.
How to get Count
I recently downloaded Umbraco 7plus the starter kit.
I then focused my attention on the MainNavigation.cshtml page.
Since I wanted intellisense in my razor view, I changed out this code
@{ var home = CurrentPage.Site(); }
@if (home.Children.Any())
to
@if (Model.Content.Children.Any())
But when the code gets to this line @childPages(childPage.Children) and calls the helper method
@helper childPages(dynamic pages), I'm getting an error, pages.Any() is null.
Question: How do I convert the pages.Any() to get the collection count. Note: Pages ResultsView shows three items (The Starter Kit, Basics, MasterClasses").
Thanks
here is the code
@if (Model.Content.Children.Any())
{
@* Get the first page in the children *@
var naviLevel = Model.Content.Children.First().Level;
@* Add in level for a CSS hook *@
<ul class="level-@naviLevel">
@* For each child page under the home node *@
@foreach (var childPage in Model.Content.Children)
{
if (childPage.Children.Any())
{
<li class="has-child @(childPage.IsAncestorOrSelf(Model.Content) ? "selected" : null)">
@if(childPage.DocumentTypeAlias == "LandingPage")
{
<span>@childPage.Name</span>
@childPages(childPage.Children)
} else {
<a href="@childPage.Url">@childPage.Name</a>
}
</li>
}
else
{
<li class="@(childPage.IsAncestorOrSelf(Model.Content) ? "selected" : null)">
<a href="@childPage.Url">@childPage.Name</a>
</li>
}
}
</ul>
}
@helper childPages(dynamic pages)
{
@* Ensure that we have a collection of pages *@
if (pages.Any())
{
@* Get the first page in pages and get the level *@
Code dies in the helper method line if (pages.Any())
Thanks Tom
Hi Tom
Have you tried using Count? Or Count()?
Like this:
/Jan
Jan:
Thanks for replying.
Count does not work on the pages object (childPage.Children) being passed in as Dynamic.
Yes, when I query Model.Content.Children.Count it returns 5, which is wrong. Since the parent object is Learn , there are only 3 children (The Starter Kit, Basics, MasterClasses").
When I break into the code, The non public member (Results View) shows 3. I just don't know the systax to write to iterate through.
Note: the object "pages" is coming in as System.Linq.OrderedEnumerable<Umbraco.Core.Models.IPublishedContent,int>}
Do you know I could cast pages or convert to so I can iterate through?
Tom
rather than if (pages.Any()) can you not use if (pages != null && pages.Any())? I'm assuming that the problem is due to pages being null which causes .Any() to throw an exception which woiuld be the same even if you change it to use Count().
That didn't work either and I know pages.Any() is valid as when I break into the code, I see 3 values in the ResultsView.
I think if you could help me convert my pages object (System.Linq.OrderedEnumerable<Umbraco.Core.Models.IPublishedContent,int>} to something??? that's of the right type, then perhpas I could iterate through.
Thanks for helping.
Tom
Can anyone help?
Personally, i hate dymanics, so i would try to accept an iEnumberable. You may need to pass a List. Give that a quick try.
Richard:
I'm curious. What would you replace dynamics with?
And can you help explain your answer? How do I convert this code @childPages(childPage.Children) to a list?
Thanks
Tom
OK I got it to work by doing changing my helper method
from this
@helper childPages(dynamic pages)
to this
@helper childPages(IEnumerable<IPublishedContent> pages)
Thanks
is working on a reply...