Copied to clipboard

Flag this post as spam?

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


  • Marcus 9 posts 99 karma points
    Jul 17, 2018 @ 13:24
    Marcus
    0

    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.

    @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?

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Jul 17, 2018 @ 20:46
    Alex Skrypnyk
    1

    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;
    
  • Marcus 9 posts 99 karma points
    Jul 18, 2018 @ 08:56
    Marcus
    0

    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 :)

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Jul 18, 2018 @ 09:01
    Alex Skrypnyk
    100

    Hi Marcus

    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);
        }
    
  • Marcus 9 posts 99 karma points
    Jul 18, 2018 @ 09:29
    Marcus
    0

    That was a spendid idea :). Thank you so much for your help!

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Jul 18, 2018 @ 09:32
    Alex Skrypnyk
    0

    You are welcome, Marcus. Have a great day.

Please Sign in or register to post replies

Write your reply to:

Draft