Getting Error 'The model item passed into the dictionary is of type'

Topics: Customizing Orchard, Writing modules
Mar 18, 2011 at 12:12 PM
Edited Mar 18, 2011 at 1:03 PM

 

Hello. Then I am trying to display view I get error

 

The model item passed into the dictionary is of type 'IShapeProxyf549647ad9c94c68aeada97b3cd05710', but this dictionary requires a model item of type 'QRCode.Models.QRCodePart'.

 

exception gets thrown \Orchard\DisplayManagement\Descriptors\ShapeTemplateStrategy\ShapeTemplateBindingStrategy.cs
  private IHtmlString Render(ShapeDescriptor shapeDescriptor, DisplayContext displayContext, HarvestShapeInfo harvestShapeInfo, HarvestShapeHit harvestShapeHit) {
            var htmlHelper = new HtmlHelper(displayContext.ViewContext, displayContext.ViewDataContainer);
            return htmlHelper.Partial(harvestShapeInfo.TemplateVirtualPath, displayContext.Value); // THIS LINE GET ERROR
        }
MY driver class looks like this the Editor works fine, writes data to DB retrieves it also.

public class QRCodePartDriver : ContentPartDriver<QRCodePart>
        {
            private readonly IHttpContextAccessor _httpContextAccessor;
            private readonly INavigationManager _nav;
  

            public Localizer T { get; set; }


            public QRCodePartDriver(INotifier notifier, IHttpContextAccessor httpContextAccessor, INavigationManager nav)
            {

                _httpContextAccessor = httpContextAccessor;
                _nav = nav;
 
            }

            protected override DriverResult Display(QRCodePart part, string displayType, dynamic shapeHelper)
            {

 
                MemoryStream ms = new MemoryStream();
           
                QM.Encode(new QRCodeInput
                {
                    qrCodeBackgroundColor = "Black",
                    qrcodeEncodeMode = "BYTE",
                    qrcodeErrorCorrect = "M",
                    qrCodeForegroundColor = "White",
                    qrCodeScale = 4,
                    qrcodeVersion = Statics.GET_CODE_VERSION("M",_httpContextAccessor.Current().Request.Url.OriginalString),
                    qrText = _httpContextAccessor.Current().Request.Url.OriginalString
                }).Save(ms, ImageFormat.Jpeg);
                //part.Image = Convert.ToBase64String(ms.ToArray());
                
                return ContentShape("Parts_QRCode",
                    () => shapeHelper.Parts_QRCode(
                       qrCodeBackgroundColor : part.qrCodeBackgroundColor,
                        qrcodeEncodeMode : part.qrcodeEncodeMode,
                    qrcodeErrorCorrect : part.qrcodeErrorCorrect,
                    qrCodeForegroundColor : part.qrCodeForegroundColor,
                    qrCodeScale : part.qrCodeScale,
                    qrcodeVersion : part.qrcodeVersion
                                         ));
            }
            //GET
            protected override DriverResult Editor(
                QRCodePart part, dynamic shapeHelper)
            {

                return ContentShape("Parts_QRCode_Edit",
                    () => shapeHelper.EditorTemplate(
                        TemplateName: "Parts/QRCode",
                        Model: part,
                        Prefix: Prefix));
            }
            //POST
            protected override DriverResult Editor(
                QRCodePart part, IUpdateModel updater, dynamic shapeHelper)
            {

                updater.TryUpdateModel(part, Prefix, null, null);
                return Editor(part, shapeHelper);
            }
  
        }

 

View structure is 

Views -> Parts -> QRCode.cshtml // not working

Views -> EditorTemplates -> Parts ->QRCode.cshtml // works

 The record is in Database, the table QRCode_QRCodePartRecord as well.

model is the same for both that might be the problem.

 

 public class QRCodePart : ContentPart<QRCodePartRecord>
    {
        [Required]
        public string qrcodeErrorCorrect { 
            get { return Record.qrcodeErrorCorrect; }
            set { Record.qrcodeErrorCorrect = value; }
        }
        [Required]
        public string qrcodeEncodeMode
        {
            get { return Record.qrcodeEncodeMode; }
            set { Record.qrcodeEncodeMode = value; }
        }
        [Required]
        public int qrcodeVersion
        {
            get { return Record.qrcodeVersion; }
            set { Record.qrcodeVersion = value; }
        }

        [Required]
        public string qrCodeBackgroundColor
        {
            get { return Record.qrCodeBackgroundColor; }
            set { Record.qrCodeBackgroundColor = value; }
        }
        [Required]
        public string qrCodeForegroundColor
        {
            get { return Record.qrCodeForegroundColor; }
            set { Record.qrCodeForegroundColor = value; }
        }
        [Required]
        public int qrCodeScale
        {
            get { return Record.qrCodeScale; }
            set { Record.qrCodeScale = value; }
        }
       // public string Image { get; set; }
        //  internal String qrcodeStructureappendOriginaldata;
    }

 

Thank you.

Mar 19, 2011 at 12:02 AM

I've got the same error, running v1.0.2.

I was trying to go through the 'Writing a content part' tutorial, creating the map part. I enabled the feature and added the part to a content type, when I click on 'New' for that content type and it tries to render the editor I get the error below. My code is straight from the tutorial except that my Module is named 'Practice'

The model item passed into the dictionary is of type 'IShapeProxy816008ec77fe4e8d8e21dc402e20ce9a', but this dictionary requires a model item of type 'Practice.Models.MapPart'

StackTrace:

   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.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
   at System.Web.Mvc.HtmlHelper.RenderPartialInternal(String partialViewName, ViewDataDictionary viewData, Object model, TextWriter writer, ViewEngineCollection viewEngineCollection)
   at System.Web.Mvc.Html.RenderPartialExtensions.RenderPartial(HtmlHelper htmlHelper, String partialViewName)
   at Orchard.Core.Shapes.CoreShapes.RenderInternal(HtmlHelper Html, TextWriter Output, String TemplateName, Object Model, String Prefix) in C:\Users\Mike\Documents\My Web Sites\Orchard\src\Orchard.Web\Core\Shapes\CoreShapes.cs:line 350
   at Orchard.Core.Shapes.CoreShapes.EditorTemplate(HtmlHelper Html, TextWriter Output, String TemplateName, Object Model, String Prefix) in C:\Users\Mike\Documents\My Web Sites\Orchard\src\Orchard.Web\Core\Shapes\CoreShapes.cs:line 338

Coordinator
Mar 19, 2011 at 12:12 AM

You probably have a mismatch between the type of the Model property that you pass into your editor shape and the type of the edit view.

Mar 19, 2011 at 12:41 AM
bertrandleroy wrote:

You probably have a mismatch between the type of the Model property that you pass into your editor shape and the type of the edit view.

Doesn't seem to be the case from what I can tell, it's Practice.Models.MapPart in both cases:

Views\EditorTemplates\Parts\Map.cshtml

@model Practice.Models.MapPart

<fieldset>
    <legend>Map Fields</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.Latitude)
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.Latitude)
        @Html.ValidationMessageFor(model => model.Latitude)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Longitude)
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.Longitude)
        @Html.ValidationMessageFor(model => model.Longitude)
    </div>

</fieldset>


Drivers\MapDriver.cs

using Orchard.ContentManagement.Drivers;
using Practice.Models;

namespace Practice.Drivers {
    public class MapDriver :ContentPartDriver<MapPart> {

        protected override DriverResult Display(MapPart part, string displayType, dynamic shapeHelper) {
            return ContentShape("Parts_Map", () => shapeHelper.Parts_Map(
                Longitude: part.Longitude,
                Latitude: part.Latitude));
        }

        //GET
        protected override DriverResult Editor(MapPart part, dynamic shapeHelper) {
            return ContentShape("Parts_Map_Edit",
                                () => shapeHelper.EditorTemplate(
                                    TemplateName: "Parts/Map",
                                    Models: part,
                                    Prefix: Prefix));
        }

        //POST
        protected override DriverResult Editor(MapPart part, Orchard.ContentManagement.IUpdateModel updater, dynamic shapeHelper) {
            updater.TryUpdateModel(part, Prefix, null, null);
            return Editor(part, shapeHelper);
        }

    }
}
Coordinator
Mar 19, 2011 at 12:47 AM

Yeah, you see that 's' after Model? That shouldn't be here.

Mar 19, 2011 at 1:11 AM

That solved it, thanks. Sometimes I love dynamic, sometimes not so much.

Mar 21, 2011 at 9:31 AM

what about me?

I tried ' Model: part ' I would get same error

I tried 1 property ' qrCodeBackgroundColor : part.qrCodeBackgroundColor'. Crashed again

In the end I remade Driver so I would not pass anything. 

    protected override DriverResult Display(QRCodePart part, string displayType, dynamic shapeHelper)
            {
                MemoryStream ms = new MemoryStream();
                QM.Encode(new QRCodeInput
                {
                    qrCodeBackgroundColor = "White",
                    qrcodeEncodeMode = "BYTE",
                    qrcodeErrorCorrect = "M",
                    qrCodeForegroundColor = "Black",
                    qrCodeScale = 4,
                    qrcodeVersion = Statics.GET_CODE_VERSION("M",_httpContextAccessor.Current().Request.Url.OriginalString),
                    qrText = _httpContextAccessor.Current().Request.Url.OriginalString
                }).Save(ms, ImageFormat.Jpeg);
                return ContentShape("Parts_QRCode",
                    () => shapeHelper.Parts_QRCode(
                   
                                         ));
            }

I still get the error

My View looks like this

@model QRCode.Models.QRCodePart

<font color = "red"> AAAAAAAAAAAAAAAAA </font>

red AAA works if I comment @model, if not and I get the same error

The model item passed into the dictionary is of type 'IShapeProxy556f32e90ed44649ac4fc0ee216efa81', but this dictionary requires a model item of type 'QRCode.Models.QRCodePart'.

QRCodePartRecord looks like this 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orchard.ContentManagement.Records;
using System.Drawing;

namespace QRCode.Models
{
    public class QRCodePartRecord : ContentPartRecord
    {

        public virtual string qrcodeErrorCorrect { get; set; }
        public virtual string qrcodeEncodeMode { get; set; }
        public virtual int qrcodeVersion { get; set; }
        public virtual string qrCodeBackgroundColor { get; set; }
        public virtual string qrCodeForegroundColor { get; set; }
        public virtual int qrCodeScale { get; set; }
    }
}

QRCodePart like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Orchard.ContentManagement;
using System.Drawing;
using System.ComponentModel.DataAnnotations;

namespace QRCode.Models
{
    public class QRCodePart : ContentPart<QRCodePartRecord>
    {
        [Required]
        public string qrcodeErrorCorrect { 
            get { return Record.qrcodeErrorCorrect; }
            set { Record.qrcodeErrorCorrect = value; }
        }
        [Required]
        public string qrcodeEncodeMode
        {
            get { return Record.qrcodeEncodeMode; }
            set { Record.qrcodeEncodeMode = value; }
        }
        [Required]
        public int qrcodeVersion
        {
            get { return Record.qrcodeVersion; }
            set { Record.qrcodeVersion = value; }
        }

        [Required]
        public string qrCodeBackgroundColor
        {
            get { return Record.qrCodeBackgroundColor; }
            set { Record.qrCodeBackgroundColor = value; }
        }
        [Required]
        public string qrCodeForegroundColor
        {
            get { return Record.qrCodeForegroundColor; }
            set { Record.qrCodeForegroundColor = value; }
        }
        [Required]
        public int qrCodeScale
        {
            get { return Record.qrCodeScale; }
            set { Record.qrCodeScale = value; }
        }
       // public string Image { get; set; }
        //  internal String qrcodeStructureappendOriginaldata;
    }
}

Please help. I am working on this 2 days already. It has to be something simple

Mar 21, 2011 at 11:22 AM

removed 

@*@model QRCode.Models.QRCodePart*@

added

<img src = "data:image/jpg;base64,@ViewData.Model.Image" />

driver:

protected override DriverResult Display(QRCodePart part, string displayType, dynamic shapeHelper)
            {
                MemoryStream ms = new MemoryStream();
                QRCodeMaker QM = new QRCodeMaker();
                QM.Encode(new QRCodeInput
                {
                    qrCodeBackgroundColor = part.qrCodeBackgroundColor,
                    qrcodeEncodeMode = part.qrcodeEncodeMode,
                    qrcodeErrorCorrect = part.qrcodeErrorCorrect,
                    qrCodeForegroundColor = part.qrCodeForegroundColor,
                    qrCodeScale = part.qrCodeScale,
                    qrcodeVersion = part.qrcodeVersion, // Statics.GET_CODE_VERSION("M", _httpContextAccessor.Current().Request.Url.OriginalString),
                    qrText = _httpContextAccessor.Current().Request.Url.OriginalString
                }).Save(ms, ImageFormat.Jpeg);
                return ContentShape("Parts_QRCode",
                    () => shapeHelper.Parts_QRCode(
                   Image: Convert.ToBase64String(ms.ToArray())
                                         ));
            }
works.

Oct 27, 2011 at 3:09 AM

Thanks mataswork it solves my problem.

Dec 2, 2011 at 8:29 AM
Edited Dec 2, 2011 at 8:34 AM

I got the same error when using Maps sample

The model item passed into the dictionary is of type 'IShapeProxy05c2300baf564db2a10b1e74537a3253', but this dictionary requires a model item of type 'Maps.Models.MapPart'.

 It works when i removed the line

@* @model Maps.Models.MapPart*@

and used like @ViewData.Model.Latitude

But i want to use strongly typed like
@Html.LabelFor(model => model.Latitude);

 Please help me how to resolve this.

Dec 6, 2011 at 7:33 AM

Sureshrajamani,

Were you able to fix the issue? Please share it.

I've the same problem with the Map Sample!

Dec 6, 2011 at 8:11 AM

If you're using shapes (i.e. shapeHelper.SomeShape(Foo:Bar)) then you can't have strongly-typed models, it's all dynamic.

If you want strong typing, use MVC views from a controller (which you can still do).

Dec 6, 2011 at 9:10 AM
Edited Dec 6, 2011 at 9:18 AM

Thanx for the feedback, but currently I do not know how to do untyped models. I simply folowed the documentation sample to create a content part. Let me post the code and maybe someone can direct me to the solution.

Note: Yes, I'm using the shapeHelper, but I've no idea what it does or what I need to change to fix this issue.

THE DRIVER:

namespace Maps.Drivers
{
    public class MapDriver : ContentPartDriver<MapPart>
    {
        protected override DriverResult Display(
            MapPart part, string displayType, dynamic shapeHelper)
        {

            return ContentShape("Parts_Map", () => shapeHelper.Parts_Map(
                Longitude: part.Longitude,
                Latitude: part.Latitude));
        }

        //GET
        protected override DriverResult Editor(
            MapPart part, dynamic shapeHelper)
        {

            return ContentShape("Parts_Map_Edit",
                () => shapeHelper.EditorTemplate(
                    TemplateName: "Parts/Map",
                    Model: part,
                    Prefix: Prefix));
        }
        //POST
        protected override DriverResult Editor(
            MapPart part, IUpdateModel updater, dynamic shapeHelper)
        {

            updater.TryUpdateModel(part, Prefix, null, null);
            return Editor(part, shapeHelper);
        }
    }
}

THE VIEW FILE:

@model Maps.Models.MapPart

<fieldset>
  <legend>Map Fields</legend>

  <div class="editor-label">
    @Html.LabelFor(model => model.Latitude)
  </div>
  <div class="editor-field">
    @Html.TextBoxFor(model => model.Latitude)
    @Html.ValidationMessageFor(model => model.Latitude)
  </div>

  <div class="editor-label">
    @Html.LabelFor(model => model.Longitude)
  </div>
  <div class="editor-field">
    @Html.TextBoxFor(model => model.Longitude)
    @Html.ValidationMessageFor(model => model.Longitude)
  </div>

</fieldset>



<img alt="Location" border="1" src="http://maps.google.com/maps/api/staticmap? 
     &zoom=14
     &size=256x256
     &maptype=roadmap
     &markers=color:blue|@Model.Latitude,@Model.Longitude
     &sensor=false" />
I keep getting the mentioned exception when I try to use the part. Any ideas?
Dec 6, 2011 at 10:37 AM

Actually that should work - with EditorTemplate you can pass in a Mdoel and use it strongly-typed. You might just need to do a full clean/rebuild of the solution.

Dec 6, 2011 at 10:54 AM

Just did a clean & rebuild of the solution. Didn't help!

If the code is correct then maybe it's an environmental problem? Are there any settings that might cause these problems?

Dec 6, 2011 at 11:00 AM

I'm gonna start over. I'm downloading the 1.3.9 source code again to start with a clean solution. And I'm gonna follow the Maps sample again. I'll post my findings here shortly.

 

Dec 6, 2011 at 11:56 AM

I'm still not getting this to work. What the f**k am I doing wrong?

Steps:

* downloaded 1.3.9 orchard source & cooked default recipe

* created a custom content part as described at http://docs.orchardproject.net/Documentation/Writing-a-content-part

* Attached the Map part to a blog content type

* When creating a new blog I get the map editor to enter longitude & latitude

* Hitting SAVE results in an exception "The model item passed into the dictionary is of type 'IShapeProxye49c1c1ad03a4ae3a083def0fc72a2d0', but this dictionary requires a model item of type 'Maps.Models.MapPart'"

The MAP is displayed correctly when I'm editing a blog. It seems that the error occurs when orchard tries to display the blog. I've placed Maps.cshtml in the folder "Views/Parts" & "Views/EditorTemplates/Parts" as described in the tutorial. Both Maps.cshtml files are identical. I find it strange that the view is working fine in one case and not in another.

Any ideas?

Dec 6, 2011 at 12:33 PM

There's the problem - The two Maps.cshtml shouldn't be identical. For a start you don't want to display the admin textboxes, right?

The EditorTemplate shape allows you to use a strongly-typed model.

However the display shape you're using isn't an editor template, and it requires a dynamic model (meaning you have to delete the @model line from that template, and you can't use some of the HTML helpers for building fields).

Dec 6, 2011 at 12:40 PM

Some extras:

I've uploaded my view file (Map.cshtml) in a previous post.

That view is actually stored in 2 places: Views/EditorTemplates/Parts and Views/Parts. The one in the EditorTemplates works perfectly. The otherone doesn't. If I simplify the view like this:

<img alt="Location" border="1" src="http://maps.google.com/maps/api/staticmap? 
     &zoom=14
     &size=256x256
     &maptype=roadmap
     &markers=color:blue|@Model.Latitude,@Model.Longitude
     &sensor=false" />

It works. But I had to remove the strong typing of the model, and also the @Html extensions! Why is strong typing the model not working here?

Dec 6, 2011 at 12:59 PM

I explained all of that in my previous comment.

There is a reason why there are two different views: because they are different, and work differently.

This is the line where you created the display shape:

 shapeHelper.Parts_Map(
                Longitude: part.Longitude,
                Latitude: part.Latitude)
It's passing in Longitude and Latitude as dynamic model properties; there is no such thing as a strongly typed model being passed in.

 

Dec 6, 2011 at 2:14 PM

Thanx for clarifying that. It all starts to make sence to me now. 

Dec 29, 2011 at 2:40 PM

I had this same problem and spent quite a while before I found this discussion. Depending on which documentation you end up going to for hints on how to figure this out, you may get some bad information. I did - I looked at the one for the map.

Unfortunately for me, I looked at this example: http://docs.orchardproject.net/Documentation/Writing-a-content-part and it directs me to put the same code into both the Parts and EditorTemplates\Parts as Map.cshtml. I noticed that there is a link on the page called "Edit this page" and wondered if it is intended that anyone reading the page and finding a mistake like this should make note of the error. I'd be happy to do that but being FAR from an expert with Orchard figured it probably wouldn't be a good idea for me to do it, although it appears I could.

I also found after further searching these two articles which had I looked there first would not have had the problem.

This one shows a good example:http://docs.orchardproject.net/Documentation/Creating-1-n-and-n-n-relations

This one also shows it correctly: http://docs.orchardproject.net/Documentation/Creating-a-module-with-a-simple-text-editor

Mar 14, 2012 at 7:52 AM

@MarkVerbraeken , sureshrajamani

I have the same problem when i did writing a content part, and i solved them (using Orchard 1.4) :

 2 Map.cshtml file are 2 difference file : the first one in Views/EditorTemplate/Parts only contains the content in the tag <fieldset> like this :

@model Maps.Models.MapPart
<fieldset>
  <legend>Map Fields</legend>
            
  <div class="editor-label">
    @Html.LabelFor(model => model.Latitude)
  </div>
  <div class="editor-field">
    @Html.TextBoxFor(model => model.Latitude)
    @Html.ValidationMessageFor(model => model.Latitude)
  </div>

  <div class="editor-label">
    @Html.LabelFor(model => model.Longitude)
  </div>
  <div class="editor-field">
    @Html.TextBoxFor(model => model.Longitude)
    @Html.ValidationMessageFor(model => model.Longitude)
  </div>
            
</fieldset>
And the other one is 
<img alt="Location" border="1" src="http://maps.google.com/maps/api/staticmap? 
     &zoom=14
     &size=256x256
     &maptype=roadmap
     &markers=color:blue|@Model.Latitude,@Model.Longitude
     &sensor=false" />
Hope this helpfull for you !


 

Apr 7, 2012 at 4:30 PM
hpnhoem wrote:

I have the same problem when i did writing a content part, and i solved them (using Orchard 1.4) :

 2 Map.cshtml file are 2 difference file : the first one in Views/EditorTemplate/Parts only contains the content in the tag <fieldset> like this :


Thanks so much for clearing this up. I fell into the same trap. The "Writing a Content Part" tutorial could be a little more explicit about this.

Coordinator
Apr 8, 2012 at 7:57 AM

Update it. It's on Github.

May 17, 2013 at 9:31 PM
Was just running into this and it turned out to be because we had a Message.cshtml in the Views directory.

This module is based upon an existing controller in an MVC site. Took a while to figure it out.