episerver

EpiServer: Get IDs of items in content area


I'm workin on a view (Razor) of a block (blockA). This block contains a ContentArea called solutions. I would like to get a list as a string of the IDs of the items in solutions.

I have tried looking in @Model.Solutions.FilteredItems. or @(((IContent)Model).ContentLink. to see if I can access a list of the IDs of all the items.

How can I get the IDs of all items in solutions as a string list?


Solution

  • You'll find the ID in the ContentLink of the ContentAreaItem, no need to cast

    To output as a comma separated list,

    // always null check contentareas in templates
    var idList = Model.ContentArea != null && Model.ContentArea.FilteredItems.Any() ? string.Join(",", Model.ContentArea.FilteredItems.Select(x => x.ContentLink.ID)) : "";
    
    <div>@idList</div>
    

    To write it out as a list

    @if (Model.ContentArea != null && Model.ContentArea.FilteredItems.Any())
    {
        <ul>
            @{
                foreach (var item in Model.ContentArea.FilteredItems)
                {
                    <li>@item.ContentLink.ID</li>
                }
            }
        </ul>
    }