传递到字典的模型项的类型是“MyType的',但本词典需要类型的模型项目”的MyType“ [英] The model item passed into the dictionary is of type 'MyType', but this dictionary requires a model item of type 'MyType'
问题描述
是的,我通过与类似的标题问题了。他们似乎都不符合我在的情况。它只是程序员发送一个错误的对象类型的视图。
这是我收到确切的错误信息:
传递到字典的模型项的类型是IS.Extensions.Models.ContactSelectList',但本词典需要类型的IS.Extensions.Models.ContactSelectList的典范项目。
块引用>我开始在左右我们的解决方案的意见和模型类运动,企图使之框状。有了这个,我的意思是,我们已经在多个MVC应用时的一些看法/模型和一些看法/型号特定于某个MVC应用程序。结果
这个问题开始后,移动ContactSelectList视图。结果
常见的观点是,它使用剃刀发电机来编译它们的项目。我有这个在我看来:
@model IS.Extensions.Models.ContactSelectList
和调试时,我可以看到我发送到的RenderPartial方法模型是同类型的:
一些图片的标题说明:
- IS是MVC Web应用程序和引用IS.Extensions
- IS.Extensions是被引用一个类库
- ContactSelectList.cshtml位于IS \\查看\\控制
- 的ContactSelectList.cs模型类位于IS.Extensions \\型号
- 在'父视图(即触发该调用的RenderPartial坐落在共同项目的看法:ZModel.Web \\查看\\控制
有没有人有任何想法是怎么回事?错误消息是有点混乱,不是一个真正的帮助..
埃里克斯评论后编辑:
包含模型类(ContactSelectList.cs)大会是动态加载。为什么?因为我认为这是有道理创造一种扩展/插件系统,其中在一定的目录下有一定的dll扩展了普通车型的一些特殊应用的几种。结果
这就是(略有修改):VAR zExtenderAssembly = Assembly.LoadFile(路径);
extenderType = zExtenderAssembly.GetTypes()了ToList(),其中(T => t.GetInterface(IZExtender)!= NULL)。.FirstOrDefault();
回报(Activator.CreateInstance(extenderType)为IZExtender).CreateBO(ContactSelectList);在ZExtender(实现IZExtender部)CreateBo'方法简单地创建一个ContactSelectList的一个新实例(它是在相同的库作为ZExtender,所以无反射需要):
公共组件CreateBO(字符串名称)
{
...
返回新ContactSelectList();
...
}
解决方案我感动扩展DLL到Web应用程序的bin目录下,并修改该dll被加载的方式。结果
我曾经通过指定.dll文件的完整路径来加载它。加载使用的Assembly.Load('myAssemblyName')
扩展DLL,使一切工作如前。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 usingAssembly.Load('myAssemblyName')
, made everything work as before.这篇关于传递到字典的模型项的类型是“MyType的',但本词典需要类型的模型项目”的MyType“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!