Errors in logs - ViewRenderings expects RenderingModel but it should expect GlassMappers Models (System.InvalidOperationException)


Hello,

Recently I came across issue on prod system,
My logs there contains errors as below:

4020 16:03:27 ERROR Application error.
Exception: System.Web.HttpUnhandledException
Message: An unhandled exception occurred.
Source: Sitecore.Mvc
at Sitecore.Mvc.Pipelines.MvcEvents.Exception.ShowAspNetErrorMessage.ShowErrorMessage(ExceptionContext exceptionContext, ExceptionArgs args)
at Sitecore.Mvc.Pipelines.MvcEvents.Exception.ShowAspNetErrorMessage.Process(ExceptionArgs args)
at (Object , Object[] )
at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
at Sitecore.Mvc.Pipelines.PipelineService.RunPipeline[TArgs](String pipelineName, TArgs args)
at Sitecore.Mvc.Filters.PipelineBasedRequestFilter.OnException(ExceptionContext exceptionContext)
at System.Web.Mvc.ControllerActionInvoker.InvokeExceptionFilters(ControllerContext controllerContext, IList`1 filters, Exception exception)
at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
at Sitecore.Mvc.Controllers.SitecoreActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
at System.Web.Mvc.Controller.<>c__DisplayClass22.<BeginExecuteCore>b__1e()
at System.Web.Mvc.Async.AsyncResultWrapper.<.cctor>b__0(IAsyncResult asyncResult, Action action)
at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
at Sitecore.Mvc.Routing.RouteHttpHandler.EndProcessRequest(IAsyncResult result)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Nested Exception

Exception: System.InvalidOperationException
Message: Error while rendering view: '/Views/Shared/_MainLayout.cshtml' (model: 'Sitecore.Mvc.Presentation.RenderingModel, Sitecore.Mvc').

Source: Sitecore.Mvc
at Sitecore.Mvc.Presentation.ViewRenderer.Render(TextWriter writer)
at Sitecore.Mvc.Pipelines.Response.RenderRendering.ExecuteRenderer.Render(Renderer renderer, TextWriter writer, RenderRenderingArgs args)
at Sitecore.Mvc.Pipelines.Response.RenderRendering.ExecuteRenderer.Process(RenderRenderingArgs args)
at (Object , Object[] )
at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
at Sitecore.Mvc.Pipelines.PipelineService.RunPipeline[TArgs](String pipelineName, TArgs args)
at Sitecore.Mvc.Presentation.RenderingView.Render(ViewContext viewContext, TextWriter writer)
at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)

Nested Exception

Exception: System.InvalidOperationException
Message: The model item passed into the dictionary is of type 'Sitecore.Mvc.Presentation.RenderingModel', but this dictionary requires a model item of type 'ProjName.Models.GlassMapper.Pages.BasePage'.
Source: System.Web.Mvc
at System.Web.Mvc.ViewDataDictionary`1.SetModel(Object value)
at System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary dictionary)
at System.Web.Mvc.WebViewPage`1.SetViewData(ViewDataDictionary viewData)
at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData)
at Sitecore.Mvc.Presentation.ViewRenderer.Render(TextWriter writer)


Im Using Sitecore 8.1 Initial Release and GlassMapper(ver. 4.0.9.60), I created my models and inserted it to views as for example: "@model BasePage"
I didn't putted models in to Renderings inside Sitecore(With GlassMapper there is no such need as I know).
While I was searching for solution I also enabled GlassMapper config: z.Glass.Mapper.Sc.ViewRender.config but this didn't helped eighter.
I Was checking ShowConfig everythings seems to be right. As well what is strange that I encountered it only on production and only in logs files, locally I dont have those Errors.

The same errors appears when website use 'master db'
If someone have some ideas about this I would be gratefull. :)

  • Hello Kamil ;)

    did you check everything from here www.glass.lu/.../UpgradingToV4 ? Especially files inside from /App_Start ? It seems it could be a problem with Glass initialization.

  • In reply to Lukasz Skowronski:

    Hello Lukasz :)
    Thank You for Your response. Initially I installed version 4 so there was no needed to perform steps from link You provided.
    I was doing update to fix it from 4.0.3.50 to 4.0.9.60 but this didnt worked as well.
  • Could be mistaken (it's Friday evening...), but don't you need to use @inherits GlassView<> instead of @model?
  • In reply to Gert Gullentops:

    Thank You for response Gert, that how it was before v4, now they extended MVC support so now instead of using:
    @inherits Glass.Mapper.Sc.Web.Mvc.GlassView<MyModel>
    You can as well use:
    @model MyModel
  • Are you using ViewRendering? if yes then your model must inherits "Sitecore.Mvc.Presentation.RenderingModel" class, Either change your model class to something that inherits that class, or use "Controller" rendering, where you can pass any model back to your view without any restrictions.

    Some good links:

    mhwelander.net/.../view-renderings-vs-controller-renderings
    www.kewlcodes.com/.../Controller-rendering-in-Sitecore-MVC-with-a-full-example
  • In reply to Ahmed Okour:

    Thank You for suggestion, I tryied Your hint but there are still errors in logs, My models iherits from GlassBase (the base class with standard fields) that I created, and this class inherits from Sitecore.Mvc.Presentation.RenderingModel.

    [SitecoreType]
    public class GlassBase : RenderingModel, IGlassBase
    {
    public GlassBase();
    
    [SitecoreInfo(SitecoreInfoType.DisplayName)]
    public virtual string DisplayName { get; set; }
    [IndexField("_fullpath")]
    public virtual string FullPath { get; set; }
    [IndexField("_group")]
    [SitecoreId]
    public virtual Guid Id { get; set; }
    .
    .
    .
    
    And my BasePage model:
    
       [SitecoreType(TemplateId = TemplatesGuids.Base_Page, AutoMap = true)]
        public class BasePage : GlassBase
        {
            [SitecoreField("Page Title")]
            public virtual String PageTitle { get; set; }
    
            [SitecoreField("Meta Description")]
            public virtual String MetaDescription { get; set; }
    .
    .
    .
  • In reply to Kamil Kleszczewski:

    In this case, Did you create a model item in sitecore that represent your "ProjName.Models.GlassMapper.Pages.BasePage" class? you will also need to select this model in your view rendering item, Because by default if you didn't select any model in view rendering, Sitecore will use 'Sitecore.Mvc.Presentation.RenderingModel' as your model object
  • In reply to Ahmed Okour:

    When using GlassMapper in version 4 You don't need to create model item inside sitecore, and You dont need to select this model in view rendering item - because MVC support was extended and you can determine the model your view renderings use from the view itself :)

    Take a look on to this link:
    www.glass.lu/.../GlassV4 (section "Models from Views")
    and this link:
    cardinalcore.co.uk/.../glass-v4-model-from-view (section "Sitecore MVC with Glass V4")

    In my situation the case is that on page the models are properly mapped (on website there is no problems when you enter urls) but the logs shows errors :/

  • As well Im using 8.1 Sitecore fallback mechanism so i think it could have some connection with those errors.
  • hey Kamil,

    is the problem solved?
    i have the same issue with Sitecore standard layouts like '/sitecore/shell/client/Speak/Layouts/Layouts/Speak-Layout.cshtml'.

    thank you!
  • In reply to Tamás Tárnok:

    Hi Tamas,

    Unfortunatelly its not solved, I have the same issue on two projects.
    It looks like its GlassMapper bug.
    I saw that this issue occurs when You enter the page that have items without current language version.

    If You will have some more information related, please share it here :)

    All the bests,
  • In reply to Kamil Kleszczewski:

    Hi Kamil,
    I'm facing the same issue only on Prod CD servers and not in CM server. Did you ever get this resolved
    Appreciate your help in advance

    Thanks,
    Vasuki Bharadwaj