How do I get a list of the children for a current page using a Model?
I tried to create a ViewModel for a landing page that had a field of type List but kept getting error for casting from Sitecore.Data.Items.Item to MyModel.
Updated:
I am able to get to get it working with a View Rendering, but wanting to create a Model so i can have strongly type object. Here is an example code of what i'm trying to do:
I'm trying to create a landing page that will return all child items that of type Article. I will probably need to do some filtering/shorting so thinking creating a Model to work with would be helpful here.
public class Article
{
public string Title { get; set; }
public string ShortSummary { get; set; }
public DateTime PublishDate { get; set; }
public Speech(Item item)
{
this.Title = item["Title"];
this.ShortSummary = item["Short Summary"];
this.PublishDate = item["Publish Date"];
}
}
public class NewsLandingViewModel : IRenderingModel
{
public string Title { get; set; }
public string ShortSummary { get; set; }
public List<Article> Articles {get; set;}
public void Initialize(Rendering rendering)
{
var dataSourceItem = rendering.Item;
this.Title = dataSourceItem["Title"];
this.ShortSummary = dataSourceItem["Short Summary"];
foreach (Item child in dataSourceItem.Children)
{
this.Articles.Add(new Article(child));
}
}
}