13

I have a control property with a data source pointing to a folder with items with fields.

Is there a way to loop through the children of the item in the data source and display them on my view?

In my view I have:

@using Sitecore.Mvc.Presentation
@using Sitecore.Mvc
@model RenderingModel

In my controller I have:

public ActionResult Index()
{
    var renderingModel = new Sitecore.Mvc.Presentation.RenderingModel();
    var myTitle = renderingModel.PageItem.Fields["Title"].Value;
    var item = Sitecore.Mvc.Presentation.RenderingContext.Current.Rendering.Item;
    var itemPageContext = Sitecore.Mvc.Presentation.RenderingContext.Current.PageContext.Item;
    return View();
}

Control Properties

enter image description here

4 Answers 4

25

Controller:

public class RotatorController : SitecoreController 
{
  public ActionResult Index()
  {
    var dataSourceId = RenderingContext.CurrentOrNull.Rendering.DataSource;
    var dataSource = Sitecore.Context.Database.GetItem(dataSourceId);

    var viewModel = new RotatorViewModel
    {
      RotatorItems = dataSource.Children
    };
    return View(viewModel);
  }
}

View Model:

public class RotatorViewModel
{
  public IEnumerable<Item> RotatorItems { get; set; }
}

View:

@model RotatorViewModel 

<h1>This is a rotator!</h1>
@foreach (var item in Model.RotatorItems)
{
  <h2>@item.Fields["Title"]</h2>
}
2
  • 2
    Don't forget to change your model on the view @model RotatorViewModel Oct 5, 2016 at 19:25
  • 1
    Nice catch. Done. Oct 5, 2016 at 19:26
5

Since you have @model RenderingModel, you could access the datasource item via Model.Rendering.Item. To loop over the child items of the datasource you would do something like this:

@foreach(Item child in Model.Rendering.Item.Children)
{
    <div>@child.Name</div>
}
3
  • This code has a red line under .Name. Cannot convert method group Name to non-delegate type object.
    – ADH
    Oct 5, 2016 at 19:21
  • 1
    You may need to be explict with the type of child and use Item rather than var. I updated the answer to show this.
    – Ben Golden
    Oct 5, 2016 at 19:30
  • Your answer seems to work for accessing the DataSource item. One difference though is that if no DataSource has been set, accessing Model.Rendering.Item resolves to the context item while Model.Rendering.DataSource will just be empty.
    – comecme
    Nov 29, 2017 at 8:47
1

Try this

Sitecore.Mvc.Presentation.RenderingContext.Current.Rendering.DataSource

You will get your datasource item (Home Page Rotator) ID.

Then you can obtain children and pass whole collection to the View via Model and display whatever you want.

1
  • How do I obtain the children?
    – ADH
    Oct 5, 2016 at 19:22
0

I access a Data Source which is a folder of content items having a Name field and a Price field. In the View Rendering, my Data Source is set to /sitecore/content/Products which is the folder containing the projects. In my View Rendering cshtml file I have the following:

@using Sitecore.Data.Items
@using Sitecore.Mvc.Presentation

<table>
    @foreach (Item product in RenderingContext.Current.Rendering.Item.Children)
     {
        <tr><td>@product["Name"]</td><td>@product["Price"]</td></tr>
     }
</table>

This accesses the data source in a simpler way. This is with Sitecore 9.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.