Using View Renderings with Glass Mapper and compiled views

Our old friend View Renderings cropped up recently when we moved our ORM related code into it’s own Helix style foundation module. First up a short history of View Renderings in Sitecore…

Traditionally when using View renderings in Sitecore, if we didn’t want to use the built in Sitecore.Mvc.Presentation.RenderingModel model for our views we’d build a custom model and ensure Sitecore itself was aware of it by creating a Model item in /sitecore/layout/Models and fill in the Model field of the View rendering.

Without this Sitecore cannot resolve the model correctly:

So far, so old.

Being forced to add a model to Sitecore felt a bit limiting, and back in 2014 John West wrote a nice article demonstrating how to remove the need for specifying the model in Sitecore (since it is already present on the top of your view) https://community.sitecore.net/technical_blogs/b/sitecorejohn_blog/posts/determine-models-from-views-with-the-sitecore-asp-net-cms

Later on, Nat Mann ran with the idea and optimised the solution for performance: https://cardinalcore.co.uk/2015/03/24/getting-a-sitecore-model-from-the-cshtml-view/

Now we can use a custom model without the need to define the model in Sitecore. Sweet!

But what if you’re using Glass Mapper…

No problem, Glass has incorporated the GetModelFromhttps://github.com/mikeedwards83/Glass.Mapper/blob/master/Source/Glass.Mapper.Sc.Mvc/Pipelines/Response/GetModelFromView.csView code into the library as of release 4.0.0.4 adding the ability to determine the model required for a View Rendering from the Cshtml file itself.

This means you can happily use your Glass models with View Renderings and again, with no Sitecore Model admin.

All straight forward, but what if your views are precompiled (as Kamsar blogs about here) and don’t actually exist on disk?

Mike Edwards has happily provided the solution here.
Incorporating this code into your GlassMapperScCustom class will allows Glass to probe compiled view assemblies and locate the view.

Leave a Reply

Your email address will not be published. Required fields are marked *