Getting parent content area in block

Vote:
 

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 ?

#182249
Sep 13, 2017 13:25
Vote:
 

Not particularly solving your problem, but you might get some hints about how to get block's index: https://github.com/valdisiljuconoks/EPiBootstrapArea/blob/master/README.md#get-block-index-in-the-contentarea

#182251
Sep 13, 2017 14:23
Vote:
 

Take a look here Marius: https://hacksbyme.net/2017/06/13/episerver-how-to-know-a-blocks-position-in-a-content-area/

#182395
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.