Batsu
Batsu

Reputation: 61

Sending list to View via Controller

New to Sitecore, and just looking to send an item's children to the view. My controller looks like this:

        Database database = Sitecore.Context.Database;

        Item myItem = database.GetItem("{0173DED8-D91D-45F0-A390-F17F3BA32A5C}");

        IEnumerable<Item> itemList = myItem.GetChildren();

        return View(itemList);

What code is needed in the View to populate this list? I'm assuming a foreach, but I really have no idea how to write it out, so an example would be most helpful.

Upvotes: 0

Views: 154

Answers (1)

Chris Auer
Chris Auer

Reputation: 1435

Simple as this.

I would recommend against sending the whole item to the view. Its a lot of data.

@model IEnumerable<Item>

@foreach (var item in Model)
{
    <span>@item.Name</span>
}

See my answer here for using Glass Mapper to send editable sitecore fields to a view from a controller rendering. Its much smaller than the entire item.

https://sitecore.stackexchange.com/questions/2795/best-practice-for-implementing-a-controller-rendering-using-glasscontroller/2803#2803

Also here is a great resource for Sitecore MVC. The Sitecore community MVC example project.

https://github.com/Sitecore-Community/sample-sitecore-mvc

Upvotes: 1

Related Questions