The model item passed into the dictionary is of ty

2019-05-14 20:47发布

问题:

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();
    ...
}

回答1:

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.