The model item passed into the dictionary is of type 'Sitecore.Mvc.Presentation.RenderingModel', but this dictionary requires a model item of type 'X'

Matthew Dresser picture Matthew Dresser · Sep 16, 2013 · Viewed 7k times · Source

I am building a solution with Sitecore 7 and ASP.NET-MVC 3 and trying to use a custom model class as described in this blog post by john west.

I have seen several other questions here on SO reporting a similar error with ASP.NET-MVC (without Sitecore), usually related to passing the wrong type of object in controller code, or there being a configuration error with the \Views\web.config file, but neither seem to be the issue here.

Answer

Tom Hawkin picture Tom Hawkin · Feb 11, 2014

this issue is caused when you create a view rendering (possibly others but i haven't tried it) and you have not set up the model in sitecore, so sitecore is passing in its default model.

To fix this you have to go to the layouts section and create a model. this is the path in sitecore '/sitecore/layout/Models/', in this folder create a 'Model' item and in the model type field you add the reference to your model in the format 'my.model.namespace, my.assembly' without the quotes.

your model needs to inherit 'Sitecore.Mvc.Presentation.IRenderingModel' which forces you to implement the 'Initialize' method, in here you populate data from the sitecore item into the properties of the model. here is an example model...

namespace Custom.Models.ContentBlocks
{
using Sitecore.Data.Fields;
using Sitecore.Mvc.Presentation;

public class BgImageTitleText : IRenderingModel
{

    public string Title { get; set; }

    public string BgImage { get; set; }

    public string BgImageAlt { get; set; }

    public string BgColour { get; set; }

    public string CtaText { get; set; }

    public string CtaLink { get; set; }

    public void Initialize(Rendering rendering)
    {
        var dataSourceItem = rendering.Item;
        if (dataSourceItem == null)
        {
            return;
        }

        ImageField bgImage = dataSourceItem.Fields[Fields.ContentBlocks.BgImageTitleTextItem.BgImage];
        if (bgImage != null && bgImage.MediaItem != null)
        {
            this.BgImageAlt = bgImage.Alt;
            this.BgImage = Sitecore.Resources.Media.MediaManager.GetMediaUrl(bgImage.MediaItem);
        }

        var title = dataSourceItem.Fields[Fields.ContentBlocks.BgImageTitleTextItem.Title];
        if (title != null)
        {
            this.Title = title.Value;
        }

        var link = (LinkField)dataSourceItem.Fields[Fields.ContentBlocks.BgImageTitleTextItem.CtaLink];
        if (link != null)
        {
            this.CtaLink = link.GetLinkFieldUrl();
        }

        var ctaText = dataSourceItem.Fields[Fields.ContentBlocks.BgImageTitleTextItem.CtaText];
        if (ctaText != null)
        {
            this.CtaText = ctaText.Value;
        }

        var bgColour = dataSourceItem.Fields[Fields.ContentBlocks.BgImageTitleTextItem.BgColour];
        if (bgColour != null)
        {
            this.BgColour = bgColour.Value;
        }
    }
}
}

Then you have to go to your view rendering (or possibly other types of rendering) and in the 'Model' field you click insert link and click on your newly created model.