Block rendering in on page edit mode - "The block can not be displayed"

Vote:
 

We have an issue with block rendering in edit mode.

We have a block which contains another block used as a property (or "local block" as defined by Joel Abrahamsson)

We are using EditAttributes() appropriately in the View to render the edit features in preview mode. However, whenever we do make a change to one of the properties of the local block in preview mode, we're receiving an error message "The block can not be displayed".

We'd expect to be able to make changes to the property in preview mode and see those changes reflect immediately as we've seen this in the past.

For example - our block which references the local block:

public class MediaBlock : BlockData
{
    [Display(
    Name = "Media Content",
    public virtual MediaContentBlock Content { get; set; }
}


The local block:

public class MediaContentBlock : SiteBlockData
{
    [Display(Name = "Test message")]
    public virtual string TestMsg{ get; set; }

}

Bog standard controller:

public class MediaBlockController : BlockController
{
    public override ActionResult Index(MediaBlock currentBlock)
    {
        return PartialView(currentBlock);
    }
}

Bog standard view:

@model MediaBlock
x.Content)> @Model.Content.TestMsg



The behaviour is as follows:

1. Create the block and view it in edit mode - the block will render fine at this point.

2. Click on the block edit attribute - change the TextMsg text to something

3. Observe the behaviour:

#171651
Edited, Nov 14, 2016 15:57
Vote:
 

The easiest (best?) way to render a block property (local block) is to:

@model MediaBlock
@Html.PropertyFor(x => x.Content, new { CustomTag = "div" })

Then you also need a view for MediaContentBlock that renders the TestMsg property.

You don't need a controller for blocks either, unless you have some extra data or computing to do to the view model. A controller will just decrease the performance.

#171673
Edited, Nov 14, 2016 19:19
Vote:
 

The reason you're getting the error is because when you're using EditAttributes (or PropertyFor for that matter) Episerver will look for a view or renderer and then inject that on the page, but since you don't have one for MediaContentBlock an error message is returned instead. Episerver will not re-render the whole page when you make changes to the property, it will just re-render that property.

Episerver doesn't know that you have @Model.Content.TestMsg inside the div with your EditAttributes. That's why the page is only rendered correctly on page load, not when the property is changed.

I wouldn't recommend rendering a block property's properties directly as you have done in your example. Instead render the block property as in my example and then add a view for that block. At least when you want to make the property editable anyway.

#171674
Edited, Nov 14, 2016 19:31
Vote:
 

Thank you Johan!

#171724
Nov 16, 2016 12:46
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.