Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Getting parent content area in block


I have a problem getting the content area of the parent page/ block in a block in view mode (in edit mode everything works fine). The main issue I'm trying to solve is to get a blocks position on a page based on it's index.

In my block controller:

public override ActionResult Index(PromotionBlock currentBlock)
            var parentContentArea = ControllerContext?.ParentActionViewContext?.ViewData?.Model as ContentArea;
            currentBlock.Position = _blockUtilities.GetBlockPositionInContentArea(parentContentArea, currentBlock);

            var model = new PromotionBlockViewModel(currentBlock);
            return PartialView(model);

In BlockUtilities

        public string GetBlockPositionInContentArea(ContentArea contentArea, BlockDataBase block)
            var blockContent = (IContent) block;
            return contentArea != null ? contentArea.FilteredItems?.ToList().FindIndex(o => o.ContentGuid == blockContent.ContentGuid).ToString() : string.Empty;

The main issue is that parantContentArea is null, when controller runs in view mode while have correct value in editor mode. When I debug ControllerContext?.ParentActionViewContext?.ViewData?.Model have the correct value, but it looks like the casting is not working. 

Any suggestions of why the casting is not working, or an other way of getting the contentArea ?

Sep 13, 2017 13:25

Not particularly solving your problem, but you might get some hints about how to get block's index:

Sep 13, 2017 14:23

Take a look here Marius:

Sep 18, 2017 10:33
* 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.