Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BlockIndex and GetDisplayOption helper does not return any value #59

Closed
yophen opened this issue Jan 17, 2020 · 10 comments
Closed

BlockIndex and GetDisplayOption helper does not return any value #59

yophen opened this issue Jan 17, 2020 · 10 comments
Assignees
Labels
Milestone

Comments

@yophen
Copy link

yophen commented Jan 17, 2020

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.

@yophen yophen changed the title BlockIndex helper and GetDisplayOption helper does not return any value BlockIndex and GetDisplayOption helper does not return any value Jan 17, 2020
@valdisiljuconoks
Copy link
Owner

Can you check in IoC (StructureMap) which Content area renderer is registered? Easily visible via Episerver Developer Tools

@yophen
Copy link
Author

yophen commented Jan 17, 2020

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.

@yophen
Copy link
Author

yophen commented Jan 17, 2020

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.

@valdisiljuconoks
Copy link
Owner

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.

@yophen
Copy link
Author

yophen commented Jan 17, 2020

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?

@valdisiljuconoks
Copy link
Owner

Yes, that's correct, no manual DI is needed.

@birgitzprinz
Copy link

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.

@valdisiljuconoks valdisiljuconoks self-assigned this Aug 31, 2020
@valdisiljuconoks valdisiljuconoks added this to the 5.3 milestone Aug 31, 2020
valdisiljuconoks added a commit that referenced this issue Aug 31, 2020
@valdisiljuconoks
Copy link
Owner

v5.3 is on the way to the feed

@jonascarlbaum
Copy link

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>;
    }
}

@valdisiljuconoks
Copy link
Owner

valdisiljuconoks commented Oct 27, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants