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

左心房为你撑大大i 提交于 2020-01-31 16:54:05

问题


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.


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/18837256/the-model-item-passed-into-the-dictionary-is-of-type-sitecore-mvc-presentation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!