Find first item in a list with a specific NodeTypeAlias
Hi
I have a menu where I need to get the first item that dont use the NodeTypeAlias "OverskriftHolder".
or said in anothe way. I always need the second item in the menu.
This is what I got so fare
@if (Model.Descendants().Any()) {
@* Display the current page as the last item in the list *@ <div id="click" class="clickLink">Sub-menu</div> <div id="slideUp" class="navSubpagesBottom"> @*<h2> Sub-menu for @Model.Name</h2>*@
<ul> @* For each page in the descendants collection which have been ordered by Level (so we start with the highest top node first) *@ @foreach (var page in Model.Descendants().OrderBy("Level")) { if(page.NodeTypeAlias != "OverskriftHolder"){
Before answering, can I ask what version of Umbraco you are using and assuming it's newer than v4.10 are you fixed to use a legacy Razor Macro rather than a recommended Partial View Macro? If you don't know what your using, could you please post the @inherits from the top of your cshtml as we can work out from that. All this could all be irrelevant if this is a strongly typed partial view......
Glad you've fixed it. Meanwhile, if you can, you should use a Partial View Macro instead of this legacy Razor Macro. Descendants() is very slow in DynamicNode (which you are using) but is fine in IPublishedContent (Partial View Macro).
Find first item in a list with a specific NodeTypeAlias
Hi
I have a menu where I need to get the first item that dont use the NodeTypeAlias "OverskriftHolder".
or said in anothe way. I always need the second item in the menu.
This is what I got so fare
Hope you can help me :-)
/Kate
Hi Kate,
You can use the .First() method to get the first item of an collection.
You can find the documentation here: http://our.umbraco.org/documentation/Reference/Querying/DynamicNode/Collections#First%28%29
Hope this helps
/Dennis
Hi Kate,
Before answering, can I ask what version of Umbraco you are using and assuming it's newer than v4.10 are you fixed to use a legacy Razor Macro rather than a recommended Partial View Macro? If you don't know what your using, could you please post the
@inherits
from the top of your cshtml as we can work out from that. All this could all be irrelevant if this is a strongly typed partial view......Jeavon
Jeavon: It says: @inherits umbraco.MacroEngines.DynamicNodeContext
Dennis: Dont think I can use .First(), because it is the second item I need
Hej Kate,
Then you can combine it with a .Next(). So your collection would be .First().Next()
So something like this:
.Hope this helps,
/Dennis
Hi Dennis
I got it to work.
But insted of using @foreach I did it like this.
And tha seems to work :-)
Thanks for your help
Hi Kate,
Glad you've fixed it. Meanwhile, if you can, you should use a Partial View Macro instead of this legacy Razor Macro.
Descendants()
is very slow in DynamicNode (which you are using) but is fine in IPublishedContent (Partial View Macro).Jeavon
Hi Jeavon
Okay, thanks.
I guess I have to start read about Partial View Macro ;-)
/Kate
This code would be identical except you would need to substitute
Model
withCurrentPage
is working on a reply...