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>
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.
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.
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)
Event 2 ( has a list of dates as nestedContent)
Now I need to render an Eventcalendar in a chronological order like so:
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
Something like this in your Events template should get you started:
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:
I hope I explained it well enough.
Ahh, so you'll want to cast those items to a list probably using ToList() then sort that.
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
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
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.
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 thehome.cshtml
like so: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
Hi,
Put your models into a models folder.
Be sure each model class has a namespace
Then ensure you reference this model in the view
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
Hi Steve,
your absolutely right.
Works as expected.
Thank you so much.
Edgar
is working on a reply...