简体   繁体   English

ASP.NET MVC:传递到词典中的模型项的类型为“ ClassA”,但是此词典需要类型为“ ClassA”的模型项

[英]ASP.NET MVC: The model item passed into the dictionary is of type 'ClassA', but this dictionary requires a model item of type 'ClassA'

I'm getting a peculiar exception using the Html.RenderPartial method. 我使用Html.RenderPartial方法得到一个特殊的异常。 ASP.NET MVC seems to be unable to an object of type ClassA to an object of type ClassA. ASP.NET MVC似乎无法将ClassA类型的对象转换为ClassA类型的对象。 I was wondering if anyone knows what's going on. 我想知道是否有人知道发生了什么事。

Here's a little more background info. 这是更多背景信息。 I'm having the following hierarchy in place: 我有以下层次结构:

public interface IInterface
{
    string Name { get; }
}

public class ClassA : IInterface
{
    public string Name
    {
        get
        {
            return "ClassA ";
        }
    }
}

Using these two in a view: 在视图中使用这两个:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<IEnumerable<IInterface>>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

<% foreach (IInterface item in Model)
   {
       Html.RenderPartial(string.Concat(item.Name, "UserControl"), item);
   }
%>

</asp:Content>

And having a UserControl named ClassAUserControl with this header: 并具有一个带有此标头的名为ClassAUserControl的UserControl:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ClassA>" %>

Edit: TypeHelpers.IsCompatibleObject(Object value) decides that the two types are different: 编辑:TypeHelpers.IsCompatibleObject(Object value)确定两种类型不同:

public static bool IsCompatibleObject<T>(object value)
{
    return ((value is T) || ((value == null) && TypeAllowsNullValue(typeof(T))));
}

In the above case T is ClassA and the type of value is ClassA. 在上述情况下,T为ClassA,值的类型为ClassA。 That really makes me wonder why 'value is T' fails... 这真的让我想知道为什么“值就是T”会失败...

Your usercontrol is expecting a ClassA Object whereas you give to it a IInterface object. 您的用户控件期望一个ClassA对象,而您给它一个IInterface对象。 He cannot downcast it to object A because he can't know it is a class A. You might do something of the kind using reflection to recast your IInterface to get back your ClassA type, but this would be so ugly I prefer not think about it another second... 他无法将其转换为对象A,因为他不知道它是A类。您可能会使用反射来进行某种操作以重铸IInterface以返回您的ClassA类型,但是这太丑了,我不想考虑再过一秒钟...

You should change: 您应该更改:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<IEnumerable<IInterface>>" %>

to

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<IEnumerable<ClassA>>" %>

It does not know that your IInterface object is also a ClassA . 它不知道您的IInterface对象也是ClassA

Or change: 或更改:

Html.RenderPartial(string.Concat(item.Name, "UserControl"), item);

to casting (will fail if there exists objects which are not ClassA 强制转换(如果存在非ClassA对象,将失败

Html.RenderPartial(string.Concat(item.Name, "UserControl"), (ClassA)item);

or properly the best solution (if you expect other types than ClassA ) 或适当的最佳解决方案(如果您希望使用ClassA以外的其他类型)

foreach (IInterface item in Model)

to

foreach (ClassA item in Model.OfType<ClassA>())

this will only go through elements of type ClassA . 这只会通过ClassA类型的元素。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 传递到字典中的模型项的类型为......,但此字典需要... - The model item passed into the dictionary is of type …, but this dictionary requires … ASP.NET词典需要NO SENSE类型的模型项 - ASP.NET dictionary requires a model item of type NO SENSE 传递到字典中的模型项的类型为“”,但是此字典需要模型类型为“”的模型项 - The model item passed into the dictionary is of type ' ', but this dictionary requires a model item of type ' ' 传递给字典的 model 项是类型的.. 但是这个字典需要一个 model 类型的项 - The model item passed into the dictionary is of type .. but this dictionary requires a model item of type 传入字典的模型项的类型为“System.Web.Mvc.HandleErrorInfo”,但此字典需要类型的模型项 - The model item passed into the dictionary is of type 'System.Web.Mvc.HandleErrorInfo', but this dictionary requires a model item of type MVC - 传递到字典中的模型项是类型 .. 但是这个字典需要一个类型为的模型项 - MVC - The model item passed into the dictionary is of type.. but this dictionary requires a model item of type MVC:传递到字典中的模型项的类型为X,但是此字典需要类型为Degreenery.models.Gebruiker的模型项。 - MVC: The model item passed into the dictionary is of type X, but this dictionary requires a model item of type Degreenery.models.Gebruiker 传递到字典中的模型项在ASP.net MVC中的类型为“ System.Collections.Generic.List…” - The model item passed into the dictionary is of type 'System.Collections.Generic.List… in ASP.net MVC ASP.Net MVC“传入字典的模型项是&#39;System.Collections.Generic.List&#39;类型” - ASP.Net MVC “The model item passed into the dictionary is of type 'System.Collections.Generic.List” 这本字典需要类型的模型项 - this dictionary requires a model item of type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM