Wednesday, May 8, 2013

Sitecore 6.6 MVC EditFrame Helper

If you ever attempted using EditFrame with MVC, you probably had to write a helper to render it out. I figured I would share an EditFrame helper code I came up with. I'm using MVC4 with Sitecore 6.6. Edit Frame seems to be rendering the same way as it does with ASP.NET Forms.

C# Helper code:


public static class EditorFrameHelper
    {
        public static EditFrame EditFrameControl;

        private class FrameEditor : IDisposable
        {
            private bool disposed;
            private HtmlHelper html;

            public FrameEditor(HtmlHelper html, string dataSource = null, string buttons = null)
            {
                this.html = html;
                EditorFrameHelper.EditFrameControl = new EditFrame
                {
                    DataSource = dataSource ?? "/sitecore/content/home",
                    Buttons = buttons ?? "/sitecore/content/Applications/WebEdit/Edit Frame Buttons/Default"
                };
                HtmlTextWriter output = new HtmlTextWriter(html.ViewContext.Writer);
                EditorFrameHelper.EditFrameControl.RenderFirstPart(output);
            }

            public void Dispose()
            {
                if (disposed) return;

                disposed = true;

                EditorFrameHelper.EditFrameControl.RenderLastPart(new HtmlTextWriter(html.ViewContext.Writer));
                EditorFrameHelper.EditFrameControl.Dispose();
            }
        }

        public static IDisposable EditFrame(this HtmlHelper html, string dataSource = null, string buttons = null)
        {
            return new FrameEditor(html, dataSource, buttons);
        }
    }

View code:

@using (Html.EditFrame(Model.Key.SitecoreItem.Paths.FullPath,"/sitecore/content/Applications/WebEdit/Edit Frame Buttons/RelatedContentSection"))
{
    <h4 id="@Model.Key.SitecoreItem.Name.HtmlAnchor()">@Model.Key.PanelType.Value</h4>
}

No comments:

Post a Comment