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

Unable to access the ContentArea containing a block

$
0
0

I have a page ContentArea that contains a list of blocks where every other block gets a certain CSS class. Do accomplish this, each block view asks what its index in the ContentArea is when it's being rendered to determine if it's an even or odd index. To do that I used the approach of this blog post:

http://world.episerver.com/blogs/Per-Magne-Skuseth/Dates/2013/8/Letting-your-blocks-know-where-they-are/

and it all works great. I'm not setting up a new site where we want to do the same thing. I copied the code for getting the ContentArea and the index straight over, but for some reason on the new site, the ParentActionViewContext property is null. It seems like for some reason on the new site, the request maybe is not considered a child request, but I can't find the difference. Here is my code:

Rendering the ContentArea property on the parent page:

@Html.PropertyFor(m => m.CurrentPage.DepartmentalSupportBlocks)


Asking for the index in the block view:

@model TLGMVCFranWeb.Models.Blocks.DepartmentalSupportBlock
@{ 
    Guid instanceID = Guid.NewGuid();
    var alignment = Html.ContentAreaIndex(((IContent)Model).ContentLink) % 2 == 0 ? "right" : "left";
   }

And the ContentAreaIndex method:

public static int ContentAreaIndex(this HtmlHelper helper, ContentReference contentLink)
{
int index = -1;
try
{
ContentArea contentArea = helper.ViewContext.ParentActionViewContext.ViewData.Model as ContentArea;
if (contentArea == null) return index;
IEnumerable<ContentAreaItem> items = EPiServer.Editor.PageEditing.PageIsInEditMode ? contentArea.Items : contentArea.FilteredItems;
index = items.Select(p => p.ContentLink).IndexOf(contentLink);
}
catch (Exception ex) 
{}
return index;
}

The only difference I see between this and the other site (other than the page and block types) are the other site uses an overidden PropertyFor to render the ContentArea in order to do some conditional work on the blocks, and it also uses a custom ContentArea renderer. But since the referenced blog uses none of that, it doesn't seem like that would be the problem here. Any idea why helper.ViewContext.ParentActionViewContext is null?


Viewing all articles
Browse latest Browse all 9076

Trending Articles