0

Sitecore 9.3

I have a view rendering where I was previously using the RenderingModel in the view,

(enter image description here)

but have switched to using the GlassMapper classes instead

(enter image description here).

This works great unless the component's data source is an unpublished item. If this happens, on the published page, I get a null exception error.

My view rendering item currently has nothing for the Model.

enter image description here

2
  • can you try to add Namespaces.NameofClass,Namespaces? in your case : bc.Templates.Project.AlphaBetaCappa.ContentTypes.Image,bc.Templates.Project.AlphaBetaCappa Jul 29, 2021 at 19:23
  • @VladIobagiu I didn't mention it in my question, but I did try that, yes. I get the same error, "could not locate type". I tried, "Abc.Templates.Project.AlphaBetaCappa.ContentTypes.Image,Abc.Templates.Project.AlphaBetaCappa.ContentTypes" and I also tried, "Abc.Templates.Project.AlphaBetaCappa.ContentTypes.Image,Abc.Templates.Project.AlphaBetaCappa"
    – Michael
    Jul 29, 2021 at 19:39

1 Answer 1

0

It seems your issue is similar to the one specified on the glass mapper issue list https://github.com/mikeedwards83/Glass.Mapper/issues/163. The workaround for the error is replacing the GetViewRenderer pipeline action:

public class GetViewRendererWithItemValidation : GetViewRenderer
    {        
        protected override Renderer GetRenderer(Rendering rendering, GetRendererArgs args)
        {           
            var viewRenderer = base.GetRenderer(rendering, args) as ViewRenderer;
            if (viewRenderer == null)
                return null;

            // Ignore item check when in page editor
            // Also this will break if the item for the datasource has been deleted without removing the link.
            if (Context.PageMode.IsPageEditor || Context.PageMode.IsPageEditorEditing)
                return viewRenderer;

            // Override renderer to null when there is an unpublished item refererenced by underlying view
            return viewRenderer.Rendering.Item != null && viewRenderer.Rendering.RenderingItem.InnerItem != null
                ? viewRenderer
                : null;
        }
    }

Add the above as a new class in your project and activate the existing glass mapper config file z.Glass.Mapper.Sc.ViewRender.config.exclude

Also if you are using glass mapper version 4 or higher you can leave the model field blank and Glass should resolve the model based on the declaration in the view rendering's cshtml file.

5
  • I am using 5.8.177 and, when I leave the model field blank, it's giving me that error if the datasource isn't published. What could I be doing wrong?
    – Michael
    Jul 30, 2021 at 14:00
  • I tried your suggested solution and it made no difference for me. It's possible I don't know how to properly implement? I just basically pasted the code into a new class file. Had to add some using statements.
    – Michael
    Jul 30, 2021 at 15:03
  • Can you try and activate the existing glass mapper config file z.Glass.Mapper.Sc.ViewRender.config.exclude? It contains a patch for the GetViewRenderer pipeline. Jul 30, 2021 at 16:22
  • That did the trick. I also suggested that as an edit to your solution to make it complete. Thank you!
    – Michael
    Aug 2, 2021 at 15:46
  • FYI, this partially resolved the issue. I still get the error when I preview a page with an unpublishable item, but on the published page, no error occurs.
    – Michael
    Aug 3, 2021 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.