View rendering with unpublished datasource item resulting in error
See original GitHub issueI’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?
Issue Analytics
- State:
- Created 8 years ago
- Comments:16 (6 by maintainers)
Top GitHub 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
Hello, It seems I have the same bug or a similar one:
Inner Exception: The model item passed into the dictionary is of type ‘Sitecore.Mvc.Presentation.RenderingModel’, but this dictionary requires a model item of type ‘Feature.Model’.
I am using Sitecore 8.2.170728 and GlassMapper 4.4.0.199. When a Datasource for View rendering is empty, Sitecore returns the Context.Item (page item) instead. I would like to get just null in normal and edit modes. So I have decided to override the Process method in GetModelFromView and to create an instance of NullModel and to assign to model for my case. I also added a new DeleteNulModel processor to the end of getModel pipeline in order to find my case and to assign null to the model. After doing some debugging until the line of code when a render renders it in ExecuteRenderer, the model is still null. But when I let it go to render it I get the error above. I don’t get when Sitecore re-assigns it. The last getModel processor set it to null for sure.
Can anyone help me please to figure out what is going on?
Thanks Andrei