Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
Hi,
I have the following code to list children of document type "carsList" in a partial view macro, and it works as just expected.
@inherits Umbraco.Web.Macros.PartialViewMacroPage @{ var numberOfVehicles = Model.MacroParameters["numberOfVehicles"].ToString() != null ? Model.MacroParameters["numberOfVehicles"].ToString() : "4"; int numberOfVehiclesInt = numberOfVehicles.AsInt(); var vehicles = Model.Content.AncestorOrSelf(1).Descendants("carsList").FirstOrDefault().Children; } @if (vehicles.Where(x => x.IsVisible()).Any()) { foreach (var vehicle in vehicles.RandomOrder().Take(numberOfVehiclesInt)) { <h2>@vehicle.Name</h2> } }
However, I would like to add vehicles to this list from a document type called "motorcyclesList". I tried by changing to following
var vehicles = Model.Content.AncestorOrSelf(1).Descendants().Where(x => x.DocumentTypeAlias == "carsList" || x.DocumentTypeAlias == "motorcyclesList").FirstOrDefault().Children;
but this still only list the cars.
Any suggestions?
Hi Marcus
Try to remove FirstOrDefault from your code:
var vehicles = Model.Content.AncestorOrSelf(1).Descendants().Where(x => x.DocumentTypeAlias == "carsList" || x.DocumentTypeAlias == "motorcyclesList").Children;
Hi Alex,
I tried remove FirstOrDefault but then I cant get the children (got the error message "Cannot assign method group to an implicitly-typed variable").
Any ideas :)
Idea:
var vehiclesFolders = Model.Content.AncestorOrSelf(1).Descendants().Where(x => x.DocumentTypeAlias == "carsList" || x.DocumentTypeAlias == "motorcyclesList"); var vehicles = new List<IPublishedContent>(); foreach (var vehiclesFolder in vehiclesFolders) { vehicles.AddRange(vehiclesFolder.Children); }
That was a spendid idea :). Thank you so much for your help!
You are welcome, Marcus. Have a great day.
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
Getting children from multiple document types in partial view macro
Hi,
I have the following code to list children of document type "carsList" in a partial view macro, and it works as just expected.
However, I would like to add vehicles to this list from a document type called "motorcyclesList". I tried by changing to following
but this still only list the cars.
Any suggestions?
Hi Marcus
Try to remove FirstOrDefault from your code:
Hi Alex,
I tried remove FirstOrDefault but then I cant get the children (got the error message "Cannot assign method group to an implicitly-typed variable").
Any ideas :)
Hi Marcus
Idea:
That was a spendid idea :). Thank you so much for your help!
You are welcome, Marcus. Have a great day.
is working on a reply...