Glass.Mapper: View rendering with unpublished datasource item resulting in error

I’m using interfaces as my viewmodels for a hybrid WebForms and MVC Sitecore project. All my view renderings have their “Model” field left empty with views having @inherits GlassView<IModel> in them to dynamically bind data to them. This approach works fine except for an edge case scenario where the datasource item defined for the view rendering is not published resulting in this “The model item passed into the dictionary is of type ‘Sitecore.Mvc.Presentation.RenderingModel’, but this dictionary requires a model item of type IModel” error.

This error happens because both GetModel and GetModelFromView result in null passing the control back to Sitecore which then passes an item of type RenderingModel to the view.

To get around this - I tried setting “Model” field on the view rendering to the type of the model (e.g. NameSpace.IModel, Website - but that results in “Constructor on type Namespace.IModel not found” error.

Can someone please advise if 1) I’m doing something wrong here 2) there is any way to get around this problem?

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 16 (6 by maintainers)

Most upvoted comments

@mikeedwards83 I did something like this in the end to handle errors and invalid datasources: http://www.hhogdev.com/blog/2015/june/mvc-rendering-exception-handler.aspx