0

IGlassBase

public class IGlassBase : RenderingModel
{
   public HtmlString Name
   {
        get
        {
            return new HtmlString(FieldRenderer.Render(this.Item, "Name"));
        }
        set
        {

        }
   }
   public HtmlString TemplateName
   {
        get
        {
            return new HtmlString(FieldRenderer.Render(this.Item, "TemplateName"));
        }
        set
        {

        }
   }
}

ArticleController

public class ArticleController : BaseController
{
    public ActionResult Listings()
    {
        IGlassBase glassBase = new IGlassBase();
        glassBase.Initialize(RenderingContext.Current.Rendering);
        return View(glassBase);
    }
}

Listings.cshtml

@model Demo_1.Models.IGlassBase
<p>Sitecore 8.2 demo web</p>
<table style="width:100%">
<tr>
    <th> Name </th>
    <th> Template Name </th>
</tr>
<tr>
    <td>
        @Model.Name
    </td>
    <td>
        @Model.TemplateName
    </td>
</tr>
</table>

When I publish this project and run it on sitecore, this error is caused:

Error Rendering View: /Views/Article/Listings.cshtml: Error while rendering view: '/Views/Article/Listings.cshtml' (model: 'Sitecore.Mvc.Presentation.RenderingModel, Sitecore.Mvc'). at Sitecore.Mvc.Presentation.ViewRenderer.Render(TextWriter writer) at Sitecore.Mvc.Pipelines.Response.RenderRendering.ExecuteRenderer.Render(Renderer renderer, TextWriter writer, RenderRenderingArgs args) 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 'Demo_1.Models.IGlassBase'. 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)

  • 2
    Sounds like your rendering is a View Rendering instead of a Controller Rendering – Chris Auer Oct 4 '17 at 15:08
  • 1
    The post has been tagged with glass-mapper, but there is nothing in this post that relates to glass-mapper. The model in this post is called IGlassBase, but it's just a class that inherits from RenderingModel. It's not an interface. – jonasag Oct 4 '17 at 15:21
  • @ChrisAuer, Let me check again and reply to you – Lee Dat Oct 4 '17 at 15:50
  • If what @ChrisAuer suggested is right then I think you need to set the model field of your view rendering to fix the immediate error: screencast.com/t/TByhjwB1G – Teeknow Oct 4 '17 at 19:40
  • @Teeknow It looks like he is trying to use a Controller Rendering (ArticleController). I only ever use the model fields when I am trying to create a View Rendering that uses IRenderingModel. I never use it for controller renderings since MVC is in control of everything. – Chris Auer Oct 4 '17 at 19:45
1

I believe your rendering is currently is the template View Rendering - /sitecore/templates/System/Layout/Renderings/View rendering

You should be using the template Controller Rendering - /sitecore/templates/System/Layout/Renderings/Controller rendering

Your controller is passing the model Demo_1.Models.IGlassBase to your rendering. This is for the use of a controller rendering. You can pull it off with a view rendering, but you have to create a model class that inherits from IRenderingModel. But that is not the case here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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