Oh God Where Am I?

Hi there!

I’m a poor little Episerver block and I’ve lost my parent, can you help me?
It is another block and for some reason I can’t do without it.

So the problem here is basically, how do I get the parent block of another block in the block controller/rendering context? Think e.g. a banner block nested within a carousel block’s content area.

It’s probably better to rethink the model and isolate the block from its parent element and not have dependencies the wrong way.
But anyway, forget about that, how do I do it?!

Well this little guy seems to do the trick, called from the Index method of the block controller when processing the child block.

private BlockData GetParentBlock()
{
    var routeDataValues = ControllerContext
                              .ParentActionViewContext
						      .RouteData.Values;
    var parentBlock = routeDataValues.ContainsKey(RoutingConstants.CurrentContentKey) 
	                      ? routeDataValues[RoutingConstants.CurrentContentKey] as BlockData 
                          : null;
    return parentBlock;
}

So, there you go. Undocumented and horrible in many ways.
Please refrain from using it. :-)

Cheers!