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

3

5

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.

Appellate answered 16/9, 2013 at 21:2 Comment(2)
Show your Controller and View codeMarbleize
The code is the same as that in the linked blog post.Appellate
A
3

This error can be caused when a controller rendering invokes a controller method which returns an ActionResult object instead of a PartialViewResult. In my case I had a rendering model associated with the layout which I believe Sitecore was trying to pass to my controller rendering.

Appellate answered 6/6, 2014 at 14:1 Comment(0)
A
4

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.

Arbour answered 11/2, 2014 at 14:19 Comment(0)
A
3

This error can be caused when a controller rendering invokes a controller method which returns an ActionResult object instead of a PartialViewResult. In my case I had a rendering model associated with the layout which I believe Sitecore was trying to pass to my controller rendering.

Appellate answered 6/6, 2014 at 14:1 Comment(0)
R
0

RenderingModel is used when you create a Rendering based on the View Rendering template. This model is created by the sitecore MVC pipelines and is automatically assigned to the view.

To have control over what model to bind to the view, you probably want to use a Controller Rendering, then you can pass in your own model from your controller.

Rebel answered 27/10, 2013 at 20:19 Comment(2)
When Sitecore adds "@model RenderingModel" to the View, my MVC4 application cannot find the default model and states "the name model does not exist in the current context". I have the correct dll references and my View's Web.config is matched up with Razor 2.0 and MVC 4.0. Any suggestions?Bengt
i think it was glass mapper that was messing it up. I had to add the inheritance from GlassView onto the View: @inherits Glass.Mapper.Sc.Web.Mvc.GlassView<Sitecore.Mvc.Presentation.RenderingModel>Bengt

© 2022 - 2024 — McMap. All rights reserved.