Created
February 1, 2017 07:23
-
-
Save blacktambourine/08da2a67b722351a85a3d6dbfff7abc4 to your computer and use it in GitHub Desktop.
Handle missing Datasource with GlassView
This file contains hidden or 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 System; | |
using Sitecore.Mvc.Presentation; | |
namespace Diversus.Core.Sitecore.ViewRenderers | |
{ | |
/// <summary> | |
/// GlassView ViewRenderings require a datasource, gracefully handle when they do not (i.e. Sitecore will try to pass in a RenderingModel when no model is found). | |
/// Handles the error: "The model item passed into the dictionary is of type 'Sitecore.Mvc.Presentation.RenderingModel', but this dictionary requires a model item of type XXXX" | |
/// </summary> | |
public class NoDatasourceSafeViewRenderer : ViewRenderer | |
{ | |
public override void Render(System.IO.TextWriter writer) | |
{ | |
try | |
{ | |
//try to render as Views that aren't GlassView ViewRenderings might not require a Datasource | |
base.Render(writer); | |
} | |
catch (InvalidOperationException ex) | |
{ | |
//If pagemode is normal then return nothing | |
if (global::Sitecore.Context.PageMode.IsNormal) | |
{ | |
} | |
else | |
{ | |
//In Experience Editor mode, render a message in a div | |
writer.Write("<div class='view-render-exception'>"); | |
writer.Write("<strong>No Datasource specified</strong>"); | |
writer.Write("</div>"); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment