Read the blog here
Last active
August 17, 2016 15:34
-
-
Save jstemerdink/6858c5a0c68aa8666b7be7272f2935d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DummyController : Controller | |
{ | |
public ActionResult Index() | |
{ | |
// Create an empty Razor view called PartialRender.cshtml in views\shared | |
return this.PartialView("PartialRender"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="FormContainerBlockControl.ascx.cs" Inherits="EPiServerWebForms.Views.Blocks.FormContainerBlockControl" %> | |
<%@ Import Namespace="EPiServerWebForms.Business.MvcHelpers" %> | |
<% MvcUtility.RenderPartial("FormsContentArea", this.FakeArea); %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public partial class FormContainerBlockControl : BlockControlBase<FormContainerBlock> | |
{ | |
protected ContentArea FakeArea { get; set; } | |
protected void Page_Load(object sender, EventArgs e) | |
{ | |
ContentArea contentArea = new ContentArea(); | |
contentArea.Items.Add(new ContentAreaItem | |
{ | |
ContentLink = this.CurrentBlock.Content.ContentLink | |
}); | |
this.FakeArea = contentArea; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ContentType(DisplayName = "FormsBlock", GUID = "5abfb24d-5fba-4637-87c5-4003f9f54536", Description = "")] | |
public class FormsBlock : SiteBlockData | |
{ | |
[Display( | |
GroupName = SystemTabNames.Content, | |
Order = 320)] | |
[CultureSpecific] | |
[AllowedTypes(typeof(FormContainerBlock))] | |
public virtual ContentArea FormContentArea { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="FormsBlockControl.ascx.cs" Inherits="EPiServerWebForms.Views.Blocks.FormsBlockControl" %> | |
<%@ Import Namespace="EPiServerWebForms.Business.MvcHelpers" %> | |
<% MvcUtility.RenderPartial("FormsContentArea", this.CurrentBlock.FormContentArea); %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@using EPiServer.Core | |
@model ContentArea | |
@Html.DisplayForModel() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class MvcUtility | |
{ | |
private static readonly ILogger Log = LogManager.GetLogger(); | |
// Render a partial view | |
public static void RenderPartial(string partialViewName, object model) | |
{ | |
if (model == null) | |
{ | |
return; | |
} | |
try | |
{ | |
HttpContextBase httpContextBase = new HttpContextWrapper(HttpContext.Current); | |
RouteData routeData = new RouteData(); | |
routeData.Values.Add("controller", "Dummy"); | |
ControllerContext controllerContext = | |
new ControllerContext(new RequestContext(httpContextBase, routeData), new DummyController()); | |
IView view = FindPartialView(controllerContext, partialViewName); | |
ViewContext viewContext = new ViewContext( | |
controllerContext, | |
view, | |
new ViewDataDictionary { Model = model }, | |
new TempDataDictionary(), | |
httpContextBase.Response.Output); | |
view.Render(viewContext, httpContextBase.Response.Output); | |
} | |
catch (InvalidOperationException invalidOperationException) | |
{ | |
Log.Critical("Cannot render partial view for: {0} \r\n {1}", partialViewName, invalidOperationException); | |
} | |
catch (NotImplementedException notImplementedException) | |
{ | |
Log.Critical("Cannot render partial view for: {0} \r\n {1}", partialViewName, notImplementedException); | |
} | |
catch (ArgumentNullException argumentNullException) | |
{ | |
Log.Critical("Cannot render partial view for: {0} \r\n {1}", partialViewName, argumentNullException); | |
} | |
} | |
// Find the view, if not throw an exception | |
private static IView FindPartialView(ControllerContext controllerContext, string partialViewName) | |
{ | |
ViewEngineResult result = ViewEngines.Engines.FindPartialView(controllerContext, partialViewName); | |
if (result.View != null) | |
{ | |
return result.View; | |
} | |
StringBuilder locationsText = new StringBuilder(); | |
foreach (string location in result.SearchedLocations) | |
{ | |
locationsText.AppendLine(); | |
locationsText.Append(location); | |
} | |
throw new InvalidOperationException( | |
string.Format("Partial view {0} not found. Locations Searched: {1}", partialViewName, locationsText)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment