1
votes

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));
        }
    }
}
1
Welcome to StackOverflow. Please provide code snippet(s) of what you have tried and what has not worked. Then someone may be able to better assist you.bland

1 Answers

2
votes

If you create a View Rendering, then Sitecore will pass a model of the type Sitecore.Mvc.Presentation.RenderingModel by default.

You can access the current Item on that model using the PageItem property and then access the Children for that item.
Here is an example view for you:

@model Sitecore.Mvc.Presentation.RenderingModel

@string.Format("Current item name = {0}, path = {1}", Model.PageItem.Name, Model.PageItem.Paths.FullPath)
<br />

@foreach (Item child in Model.PageItem.Children)
{
  @string.Format("Child name = {0}, path = {1}", child.Name, child.Paths.FullPath)
  <br />
}

That will output the name & path of the current item and all its children.

Update after you posted your code:

Change your Initialize method so it accepts an Item:

public void Initialize(Item dataSource)
{
  this.Title = dataSource["Title"];
  this.ShortSummary = dataSource["Short Summary"];

  foreach (Item child in dataSource.Children)
  {
    this.Articles.Add(new Article(child));
  }
}

Then keep using a View Rendering and use this to create your strongly typed model:

@model Sitecore.Mvc.Presentation.RenderingModel

@{
  var PageModel = new NewsLandingViewModel(Model.Item);
}

<h1>@PageModel.Title<h1>
<p>@PageModel.ShortSummary</p>

<ul>
  @foreach (Article child in PageModel.Articles)
  {
    <li>@child.Title</li>
  }
</ul>

This is one way of doing it, but isn't very flexible.
It also doesn't allow the user to edit the content using the Page Editor.

What you really want to look into is Glass Mapper for Sitecore.
It's an ORM that maps your Sitecore templates to C# Model classes.

I strongly advice you take some time studying the Glass Tutorials and you use it in your solution!