I have a page with a ContentArea:
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:
@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:
@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
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;
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.
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)
var mainBlock = contentLoader.Get<IContentData>(item.ContentLink) as MainBlock;
if (mainBlock != null)
totalItemCount += mainBlock.ContentArea.FilteredItems.Count;
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)...
Sorry for bad code sample, "built-in compiler in my head" is out-dated and needs to be updated :)