Try our conversational search powered by Generative AI!

Iterating through ContentArea items

Vote:
 

I wanted to iterator (foreach) through the items of a content area and do a Html.propertyFor() on each of them.

Below example is from a link-list

 

@foreach (PageData questionPage in Model.Links.ToPages())
{
var questPage = questionPage;
<li>
@Html.PropertyFor(x => questPage)
</li>
}

#82303
Mar 10, 2014 14:28
Vote:
 

Hi!

The PropertyFor method is a way to render a single property but a Content Area contains a list of links to content items. What are you trying to achieve? Do you want to create links for each item in the Content Area? 

#82308
Mar 10, 2014 15:36
Vote:
 

I want to render each block in my Content area ( I limit by content area to a specific type)

Since the contentArea is blocks only I should be able to use PropertyFor 

 

#82309
Mar 10, 2014 15:39
Vote:
 

Try following code:

@{
    var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
    if (page != null && page.ContentArea != null && page.ContentArea.Items.Any())
    {
        foreach (var contentItem in page.ContentArea.Items)
        {
            var item = contentLoader.Get<YOUR_CONTENT_TYPE>(contentItem.ContentLink);
// output necessary properties from content instance } } }

    

In each iteration variable `item` should point to concrete content type instance in your ContentArea.

#82363
Mar 11, 2014 11:37
gabed123 - Jul 02, 2019 18:25
Does this require a certain directive or assembly reference?
Vote:
 

if (CurrentPage.MainContentArea.Items.Any()) { foreach (var c in CurrentPage.MainContentArea.Items) { Response.Write("ContentArea Items" + c.GetContent().Name); } }
#84847
Edited, Apr 08, 2014 11:56
Vote:
 

Valdis Iljuconoks Thank you, that's exactly what I was looking for.

#89716
Aug 22, 2014 15:54
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.