Copied to clipboard

Flag this post as spam?

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


  • Edgar Rasquin 326 posts 925 karma points
    May 12, 2021 @ 16:50
    Edgar Rasquin
    0

    Get children and their nested content in one list

    Hi,

    I have the following structure in my website:

    Home

    • Events

      • Event 1 ( has a list of dates as nestedContent)

        • Date 1
        • Date 2
        • Date 3
      • Event 2 ( has a list of dates as nestedContent)

        • Date 4
        • Date 5
        • Date 6

    Now I need to render an Eventcalendar in a chronological order like so:

    • Event 1
    • Date 1
    • Date 2
    • Event 2
    • Date 4
    • Date 3
    • Date 5
    • Date 6

    So I would need to get all children of 'Events' and their 'Dates' in one list to sort them in a chronological order.

    Or is their a better solution?

    Thanks

  • Amir Khan 1287 posts 2744 karma points
    May 12, 2021 @ 20:47
    Amir Khan
    0

    Something like this in your Events template should get you started:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    
    @{ var selection = @Model.Children.Where("Visible"); }
    
    <ul>
        @if(selection.Any()) {
            @foreach(var item in selection.Where("Visible")) {
                <li>@item.Name</li>
                @{
                    var nestedContent = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("nestedContent");
                    if(nestedContent != null) {
                        foreach (var contentItem in nestedContent) {
                            <li>contentItem.GetPropertyValue("eventDate")</li>
                            // this is where you access the individual nested content items, do whatever here
                        }
                    }
                }
            }
        }
    </ul>
    
  • Edgar Rasquin 326 posts 925 karma points
    May 13, 2021 @ 08:05
    Edgar Rasquin
    0

    Hi Amir,

    thanks for your reply.

    At the moment I am doing it exactly as you described it. But this way it is not possible to get the sort order as described in my original post.

    The problem is, that once you are in the list of nested items, you can not get a parent item in between:

    enter image description here

    I hope I explained it well enough.

  • Amir Khan 1287 posts 2744 karma points
    May 13, 2021 @ 15:38
    Amir Khan
    0

    Ahh, so you'll want to cast those items to a list probably using ToList() then sort that.

  • Edgar Rasquin 326 posts 925 karma points
    May 17, 2021 @ 12:12
    Edgar Rasquin
    0

    Thanks Amir,

    that sounds like a solution.

    I have not done that before. Is there any chance you could drop a few lines of code so I have a starting point?

    Edgar

  • Steve Morgan 1349 posts 4458 karma points c-trib
    May 17, 2021 @ 16:55
    Steve Morgan
    100

    I would

    1) Create a model called EventDateViewModel this will hold both your event nodes and the date instances - add to this any of the properties that you need

    public class EventDateViewModel
    {
        public string Title { get; set; }
        public string Url { get; set; }
        public DateTime EventStartDate { get; set; }
    }
    

    Create a list of these var event dates = new List<>

    Then add your events to this list (select and use linq to populate this list) - then order by EventStartDate and output this.

    Something like:

    events = eventNodes.Select(x => new EventDateViewModel { Title = x.Name, Url = x.Url, EventStartDate = x.EventStartDate }).ToList

    then look through each eventNode and add to the list each date instance.

  • Edgar Rasquin 326 posts 925 karma points
    Jun 07, 2021 @ 13:14
    Edgar Rasquin
    0

    Hi Steve,

    sorry for my delayed reply. Thank you very much for your answer. I have now managed to work with a List<> as you suggested.

    It works very well.

    My only problem is to outsource the EventDateViewModel. For the time beeing I am including it in the home.cshtml like so:

    @functions {
    public class EventListe
        {
            public string Title { get; set; }
            public string Subtitle { get; set; }
            public string TicketLink { get; set; }
            public string Category { get; set; }
            public string Url { get; set; }
            public DateTime EventStartDate { get; set; }
            public DateTime EventStartTime { get; set; }
            public bool Schulervorstellung { get; set; }
            public bool EventSchule { get; set; }
        }
    }
    

    I tried to place it in a seperate class but VS intellisense could not asign it.

    I am not shure what I am doing wrong.

    Thanks, Edi

  • Steve Morgan 1349 posts 4458 karma points c-trib
    Jun 07, 2021 @ 15:03
    Steve Morgan
    1

    Hi,

    Put your models into a models folder.

    Be sure each model class has a namespace

    namespace EdgarsNamespace.Models
    {
        public class EventListe
        {
    

    Then ensure you reference this model in the view

    @using EdgarsNamespace.Models
    

    Remember to build your solution if you make changes to the models as this is compiled code - if you're just used to working directly in Views you'll have to remember to do this.

    Steve

  • Edgar Rasquin 326 posts 925 karma points
    Jun 07, 2021 @ 16:27
    Edgar Rasquin
    0

    Hi Steve,

    your absolutely right.

    Works as expected.

    Thank you so much.

    Edgar

Please Sign in or register to post replies

Write your reply to:

Draft