简体   繁体   中英

The model item passed into the dictionary is of type 'MyType', but this dictionary requires a model item of type 'MyType'

Yes, I went through the questions with similar titles. None of them seem to match the situation I'm in. It's simply the programmer sending a wrong object type to the view.

This is the exact error message I'm receiving:

The model item passed into the dictionary is of type 'IS.Extensions.Models.ContactSelectList', but this dictionary requires a model item of type 'IS.Extensions.Models.ContactSelectList'.

I started moving around views and model classes in our solution in an attempt to make it 'framework-like'. With this I mean that some views/models we have are used in multiple MVC applications and some views/models are specific for a certain MVC application.
The issue started áfter moving the ContactSelectList view.
Common views are in a project that uses the Razor generator to compile them.

I have this in my view:

@model IS.Extensions.Models.ContactSelectList

and when debugging I can see that the model I'm sending to the RenderPartial method is of THE SAME TYPE:在此处输入图像描述

Some sidenotes:

  • IS is a MVC web app and references IS.Extensions
  • IS.Extensions is a class library referenced by IS
  • ContactSelectList.cshtml is located in IS\Views\Controls
  • The ContactSelectList.cs model class is located in IS.Extensions\Models
  • The 'parent view' (the view that triggers this RenderPartial call is situated in the 'common' project: ZModel.Web\Views\Controls

Does anyone have any idea what's going on here? The error messages is kinda confusing and not really a help..

Edit after Erics comment:

The assembly containing the model class (ContactSelectList.cs) is dynamically loaded. Why? Because I thought it makes sense to create a kind of extension / plugin system, where dropping a certain dll in a certain directory extends the common models with some application specific ones.
This is how (slightly modified):

var zExtenderAssembly = Assembly.LoadFile(path);
extenderType = zExtenderAssembly.GetTypes().ToList().Where(t => t.GetInterface("IZExtender") != null).FirstOrDefault();
return (Activator.CreateInstance(extenderType) as IZExtender).CreateBO("ContactSelectList");

The 'CreateBo' method in the ZExtender (implements IZExtender) simply creates a new instance of a ContactSelectList (which is in the same library as the ZExtender, so no reflection needed):

public Component CreateBO(string name)
{
    ...
            return new ContactSelectList();
    ...
}

I moved the extension dll to the bin directory of the web application and changed the way the dll is loaded.
I used to load it by specifying the full path to the.dll file. Loading the extension dll using Assembly.Load('myAssemblyName') , made everything work as before.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM