Display total count of items in ContentArea in another view

Vote:
 

I have a page with a ContentArea:

@using EPiServer.Core
@using EPiServer.Web.Mvc.Html

@model AlloyTrainingNoDemo.Models.Pages.StartPage

The number of items in MainBlocks contentarea is: >> Display total count here <><>

@Html.PropertyFor(m => m.MainContentArea)

In MainContentArea I place a MainBlock which too has a ContentArea:

@using EPiServer.Core
@using EPiServer.Web.Mvc.Html

@model AlloyTrainingNoDemo.Models.Blocks.MainBlock

@Html.PropertyFor(m => m.ContentArea)

And finally inside this ContentArea i place several blocks - in this example called SubBlocks which only has a string property:

@using EPiServer.Core
@using EPiServer.Web.Mvc.Html

@model AlloyTrainingNoDemo.Models.Blocks.SubBlock
    
@Html.PropertyFor(m => m.Name)

I know that I can count the number of blocks inside the ContentArea of MainBlock using @Model.ContentArea.Items.Count(), but how can I display this total count in the view for my page fx. in the heading as shown in the code snippet for StartPage.

Thanks in advance

#151100
Jul 11, 2016 21:12
Vote:
 

On eof the hacky way would be to iterate over MainContentArea.Items and check if Item is of type "MainBlock", then cast that item to "MainBlock" and then count items inside "MainBlock". This is hacky, as code has to have knowledge about particular type inside ContentArea. Pseudo code (written from head):

foreach(var item in Model.MainContentArea.FilteredItems)
{
    var mainBlock = item as MainBlock;
    if(mainBlock != null)
    {
        itemCount = mainBlock.ContentArea.FilteredItems.Count;
        break;
    }
}
#151204
Jul 13, 2016 11:42
Vote:
 

Thanks for your reply Valdis

However, I get an error:

Cannot convert type 'EPiServer.Core.ContentAreaItem' to 'AlloyTrainingNoDemo.Models.Blocks.MainBlock' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion"

Also it says that itemCount doesn't exist in the current context.

Any ideas?

#151313
Jul 14, 2016 17:03
Vote:
 

You need to get the actual block from the reference on the item using contentloader. You also need to define a counter. Should be something like:

            var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
            var totalItemCount = 0;
            foreach (var item in currentPage.MainContentArea.FilteredItems)
            {
                totalItemCount++;
                var mainBlock = contentLoader.Get<IContentData>(item.ContentLink) as MainBlock;
                if (mainBlock != null)
                {
                    totalItemCount += mainBlock.ContentArea.FilteredItems.Count;
                    continue;
                }
            }

I'm not really sure if you want to also count the MainBlock or not...depending if you do or not you can move the totalItemCount++ to after the if(mainBlock !=null)...

#151314
Edited, Jul 14, 2016 17:51
Vote:
 

Sorry for bad code sample, "built-in compiler in my head" is out-dated and needs to be updated :)

#151316
Jul 14, 2016 19:41
Vote:
 

:)

#151317
Jul 14, 2016 20:04
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.