Choosing the right property type for blocks to allow editing


I am building my first EPiServer site and running into a problem. I have a section on a page where I want to allow a single instance of a block. I create ContentReference property and limit it to a specific type of block. When I output it in the view using PropertyFor, I do not see any way to enable on-page editing of that block. At least I would have expected the blue line around it that would give me a menu to edit/remove the block, but nothing. I even applied EditAttributes to a div around to no avail. What am I doing wrong? Does that edit experience only work with ContentArea? Below is a how I have it implemented on my page class, from which I am using IContentLoader to load the reference into my view model:

    GroupName = SystemTabNames.Content,
    Name = "Hero (Reference)",
    Order = 20)]
public virtual ContentReference Hero { get; set; }
Apr 24, 2019 21:03

Hi Jason,

Could you share the code from your view which you're using to render the content? I've tried with an alloy site and, using the following code, I can render the block referenced by the ContentReference and, in edit mode, I get a blue line around the block allowing me to select a block (you won't be able to edit the fields inside the block directly).

<div @Html.EditAttributes(x => x.CurrentPage.Hero)>
    @{Html.RenderContentData(ServiceLocator.Current.GetInstance<IContentLoader>().Get<IContentData>(Model.CurrentPage.Hero), false);}

A couple of notes about the code above (apologies if they're obvious). Don't do the content reference conversion in your view - put it in your controller and pass it to your view in a view model. Also, where possible, use constructor injection in your controller rather than using ServiceLocator.

Beyond that, I'd be tempted to use a content area to select your hero block. Aside from making it easier to render and make editable, content areas allow you to apply personalisation to blocks so that, should the need arise, you can tailor the hero based on criteria your editors set. If you're concerned about limiting it to a single item, there are ways you can keep that limitation as I've written about here:

Apr 24, 2019 22:13

Thanks for the reply. I already had the viewmodel with the block loaded from the reference and was using PropertyFor on that instance expecting it to be editable. I later realized I needed to wrap that in a div with EditAttributes for the original content reference property on the page, which enabled editing. I am not a fan of that for the reasons you state, I like the simplicity of the ContentArea or making the block a property on the page directly better. It all depends on how I want editors to interact with it, whether I want it to be reused, and whether I want personalization or not. Thanks for the help, now I know what my options are.

Apr 24, 2019 22:37
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.