Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
AI OnAI Off
Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
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
Take a look here Marius: https://hacksbyme.net/2017/06/13/episerver-how-to-know-a-blocks-position-in-a-content-area/
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 ?