-1

I am using EPiServer 7.5+ and have a scenario where some of my Local Blocks are not updating after being edited in the On Page Editing window.

I either have to do a full page refresh after editing the impacted Local Block, or use the FullRefreshPropertiesMetaData, which just refreshes the whole page anyway.

Why are some Local Blocks not refreshing correctly?

After investigating a bit I have noticed that this issue only impacts Local Blocks that have their own Controller.

Example Code

Pages / Blocks:

[ContentType("nguid")]
public class SimpleLocalBlock : BlockData{
    public virtual string Text {get;set;}
}

[ContentType("nguid")]
public class AdvancedLocalBlock : BlockData{
    public virtual string Text {get;set;}
} 

[ContentType("nguid")]
public class MyPage : PageData
{
   public virtual SimpleLocalBlock Simple {get;set;}
   public virtual AdvancedLocalBlock Advanced {get;set;}
}

Controller:

public class AdvancedLocalBlockController : BlockController<AdvancedLocalBlock>
{
    public override ActionResult Index(MultiLinksBlock currentContent)
    {
        return PartialView(ViewPath, currentContent);
    }

    //Use ViewData["Tag"] to decide which View to use
    public virtual string ViewPath
    {
        get
        {
            var tag =
                this.ControllerContext.ParentActionViewContext.ViewData["Tag"]
                        as AdvancedViews?;

            if (tag.HasValue)
            {
                switch (tag)
                {
                    case AdvancedViews.View1:
                        return "~/Views/Shared/Blocks/View1.cshtml";

                    case AdvancedViews.View1:
                        return "~/Views/Shared/Blocks/View2.cshtml";
                 }
            }

            throw new Exception("Invalid Tag.");
        }
    }

Enum for controlling which View is used:

public Enum AdvancedViews
{
    View1 = 1,
    View2 = 2
}

View (Razor) - MyPage

@model MyPage 

@Html.PropertyFor(x => x.Simple)

@Html.PropertyFox(x => x.Advanced, new {Tag = AdvancedViews.View1})

View (Razor) - SimpleLocalBlock

@model SimpleLocalBlock

@Html.PropertyFor(x => x.Text)

View (Razor) - View1

@model AdvancedLocalBlock

@Html.PropertyFor(x => x.Text)

Summary

I can use On Page Editing when editing Simple and the block will refresh correctly inline on the page (ie doesn't need to refresh entire page). But when I use On Page Editing with 'Advanced' I don't see the updates until I refresh the entire page.

Philip Pittle
  • 11,821
  • 8
  • 59
  • 123

1 Answers1

1

After using Fiddler to inspect what EPiServer was doing after an On Page Edit, I realized AdvancedLocalBlockController was throwing an exception because EPi wasn't returning the ViewData correctly.

My View Dictionary key Tag was lower cased to tag and the Enum value (int) was passed instead of an instance of the Enum.

I ended up creating a helper to support EPi:

    public static T? ParseEnum<T>(this ViewDataDictionary d, string key)
        where T : struct
    {
        var dictObject = d[key];

        if (null == dictObject)
            return null;


        if (dictObject is T?)
            return dictObject as T?;

        T parsedEnum;
        if (Enum.TryParse(dictObject.ToString(), out parsedEnum))
            return parsedEnum;

        return null;
    }

and then updated my Controller:

var tag =
     this.ControllerContext.ParentActionViewContext.ViewData
         .ParseEnum<AdvancedViews>("tag");

After that my AdvancedLocalBlock behaved correctly in EPi On Page Editing

Philip Pittle
  • 11,821
  • 8
  • 59
  • 123