BlockIndex and GetDisplayOption helper does not return any value
Closed this issue · 10 comments
I'm trying to get the block index and selected display option for a block, but the block index always returns -1 och GetDisplayOption() returns an empty string.
I'm using in the html helpers in the view, like so:
@Html.GetDisplayOption(Model)
@Html.BlockIndex()
Any idea why this is?
I'm using Episerver 11.13.2.
Can you check in IoC (StructureMap) which Content area renderer is registered? Easily visible via Episerver Developer Tools
Thanks for the reply!
When I try to install the DeveloperTools in the Episerver project using nuget the project builds ok but I get an error saying that the file could not be loaded when loading the website. Using version 3.5. Tried using Alloy with Episerver 11.12 as well.
"Could not load file or assembly 'EPiServer.DeveloperTools' or one of its dependencies. The system cannot find the file specified."
Regarding the EPiBootstrapArea, it seems to be the correct content area renderer registered since other EPiBootstrapArea functionality is working.
I tried to install EPiBootstrapArea using an empty Alloy website and there it seems to be working, at least in the basic configuration. I'll try and troubleshoot if there's any EPiBootstrapArea configuration that might interfere.
Ok that's weird for developertools. I'll take a look later.
If it's working correctly in alloy - might be some config in your project.
Do you advanced-cms content review plugin? if so - you might need to jump to latest there as well.
No, not using the advanced-cms content review plugin but thinking about it. Thanks for the tip.
The thing that seems to happen in the BlockIndex htmlhelper is that htmlHelper.ViewData does not contain any keys so looking for the "BootstrapContentArea__BlockIndex" will return -1.
Regarding dependency injection, I take it that BootstrapAwareContentAreaRenderer sets up it's own DI in the SetupBootstrapRenderer class, so no manual DI configuration is needed, is this correct?
Yes, that's correct, no manual DI is needed.
For me changing the code like this make it works again:
public static DisplayOption GetDisplayOption(this HtmlHelper htmlHelper, BlockData block)
{
if(htmlHelper == null)
throw new ArgumentNullException(nameof(htmlHelper));
if(block == null)
throw new ArgumentNullException(nameof(block));
return htmlHelper.ViewContext.ParentActionViewContext.ViewData.ContainsKey(Constants.CurrentDisplayOptionKey)
? htmlHelper.ViewContext.ParentActionViewContext.ViewData[Constants.CurrentDisplayOptionKey] as DisplayOption
: null;
}
I guess it's because in our project, we're using dedicate block controller instead of just registering a block with a partial view.
v5.3 is on the way to the feed
If others have similar issues, it could be custom block controllers messing this up.
I ended up reading the value in the controller and extending the BlockViewModel
.
Maybe there is some way to pass ViewData
from ParentActionViewContext
on to make the Html.GetIndex()
-helper work, but didn't investigate it any further.
[TemplateDescriptor(Default = true, Inherited = true)]
public class DefaultBlockController : BlockController<BaseBlockData>
{
public override ActionResult Index(BaseBlockData currentBlock)
{
var model = CreateModel(currentBlock);
var blockName = currentBlock.GetOriginalType().Name;
model.BlockIndex = int.Parse(ControllerContext.ParentActionViewContext.ViewData["BootstrapContentArea__BlockIndex"]?.ToString() ?? "-1");
return PartialView(string.Format("~/Features/Blocks/{0}/{1}.cshtml", blockName, blockName), model);
}
private static IBlockViewModel<BlockData> CreateModel(BlockData currentBlock)
{
var type = typeof(BlockViewModel<>).MakeGenericType(currentBlock.GetOriginalType());
return Activator.CreateInstance(type, currentBlock) as IBlockViewModel<BlockData>;
}
}