[英]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.