Quantcast
Channel: Developer to developer
Viewing all articles
Browse latest Browse all 9076

Obtaining the ID of a parent block from within the child block

$
0
0

Hi All,

I've been reading various posts on this forum in an attempt to figure out a way to obtain the ID of a block's parent, however most of these posts appear to be for EPiServer 6 & 7. I am working with EPiServer DXC and am trying to get the ID of the block's parent to then pass into my view as a value to be assigned to a data attribute.

I have seen several pieces of advice against looking outside of the scope of the block, however in my specific case, I can't seem to find another way to achieve this.

I am currently using the foundation.js "sticky" element, and to anchor it to another element I require the ID of said element. However, this has to be in place before the DOM is readied, I have attempted (successfully) to retrieve and insert the correct ID via JS, after the DOM is ready and while the property correctly updated, the item refused to "stick"

My current workaround is to get the user who creates the block in the CMS to enter the numerical part of the ID of the parent into a field, which is then passed through to the view on page build and while it works, I am aware this is by no means best practice and is more of a test to confirm that the ID can be provided by the Model.

In an attempt to retrieve this ID programmatically, I have used the following code:

var anchorid = "block" + (Model as EPiServer.Core.IContent).ParentLink.ID;

However it returns the ID of the "block" view folder. I found that I could decrement the retrieved value to 1 to get the ID of the parent, but this only applies in the case that the block sits within the parent's content folder, not elsewhere.

Below is the HTML code that I am using to create the sticky block:

<div class="inner" style="padding:0px;"><div class="cell" data-sticky-container><div class="sticky" data-sticky data-top-anchor="@anchorid" data-btm-anchor="@botAnchor" data-margin-top="3.25" data-sticky-on="small"><img class="image1" src="@Url.ContentUrl(Model.HeadingImage)" alt="@altText" /></div></div></div>

Is there any way that I can get the ID of the parent block and insert it into the "data-top-anchor" attribute before the DOM is readied?


Viewing all articles
Browse latest Browse all 9076

Trending Articles