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!