Skip to content

Instantly share code, notes, and snippets.

@benmccallum
Last active August 3, 2016 04:37
Show Gist options
  • Save benmccallum/f3769f2310551379e277cd179edf668f to your computer and use it in GitHub Desktop.
Save benmccallum/f3769f2310551379e277cd179edf668f to your computer and use it in GitHub Desktop.
EPiServer - Detecting Block editing
@using EPiServer.Core
@using EPiServer.Editor
@using EPiServer.Web.Mvc.Html
@model MyWebsite.Models.BlockTypes.EmergencyNotificationBlock
<div class="card-static full">
<div class="emergency-notification">
<div class="icon @Model.AlertType.ToString().ToLower()"></div>
<div class="wrapper">
<h2>@Html.PropertyFor(m => m.Title)</h2>
<p>@Html.PropertyFor(m => m.Copy)</p>
<a href="@Model.LinkPage.Uri" target="_blank" class="cta">More Details</a>
</div>
</div>
</div>
@if (ViewBag.IsInBlockPreview == true)
{
<div class="page-editing">
<strong>Alert type: </strong>
@Html.PropertyFor(m => m.AlertType)
<br />
<strong>Link page:</strong>
@Html.PropertyFor(m => m.LinkPage)
</div>
}
using EPiServer.Web.Mvc;
namespace MyWebsite.Controllers
{
/// <summary>
/// Preview controller base.
/// </summary>
public abstract class PreviewController : ActionControllerBase
{
public PreviewController()
{
// Set a flag for block views to use to detect if the block is being edited vs being rendered in a content area when the whole page is being edited
ViewBag.IsInBlockPreview = true;
}
}
}
using EPiServer;
using EPiServer.Core;
using EPiServer.Framework.DataAnnotations;
using EPiServer.Framework.Web;
using EPiServer.Web;
using EPiServer.Web.Mvc;
using MyWebsite.Models.BlockTypes;
using MyWebsite.Models.PageTypes;
using MyWebsite.Models.ViewModels;
using System.Web.Mvc;
namespace MyWebsite.Controllers
{
[ContentOutputCache(Disable = true, NoStore = true, VaryByParam = "*", Duration = 0)]
[TemplateDescriptor(
Inherited = true,
TemplateTypeCategory = TemplateTypeCategories.MvcController,
Tags = new[] { RenderingTags.Preview, RenderingTags.Edit },
AvailableWithoutTag = false)]
public class PreviewDashboardCardController : PreviewController, IRenderTemplate<DashboardCardBlockBase>
{
public ActionResult Index(IContent currentContent)
{
var model = new DashboardPreviewModel(new MyWebsiteBasePage(), currentContent);
return View(model);
}
}
}
@benmccallum
Copy link
Author

page being edited
block being edited

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment