I've got a viewpage with the inherits part looking like
我有一个继承部分看起来像的视图
Inherits="System.Web.Mvc.ViewPage<User>"
and a ViewUserControl with the inherits part looking like
和ViewUserControl一样,继承部分看起来像
Inherits="System.Web.Mvc.ViewUserControl<Address>
where User class (shortened) essentially looks like:
用户类(缩写)基本上如下所示:
class User {
public virtual Address address { get; set; }
}
Now, if I try:
现在,如果我尝试:
Html.RenderPartial("Address", Model.address);
then I potentially get some strange behaviour. In my case, it turns out Model.address was null Instead of passing null to the control, it looks like the framework tried to pass Model (i.e type=User) and was throwing an error (unexpected type).
然后我可能会得到一些奇怪的行为。在我的例子中,事实证明Model.address是null而不是将null传递给控件,它看起来像框架试图传递模型(即type = User)并抛出错误(意外类型)。
Disregarding the fact that my address was null, is that behaviour of defaulting to the parent object a bug, or expected behaviour. If it is expected behaviour then can someone please explain why? I don't get it.
忽略我的地址为空的事实,是违反父对象的行为,或者是预期的行为。如果是预期的行为,那么有人可以解释原因吗?我不明白。
2 个解决方案
#1
Very often these kinds of questions result in the OP making a mistake elsewhere, and the error isn't reproducible. I tried recreating your exact scenario, and what do you know? You're actually right :) I created a simple Parent / Child relationship and had the View be of type child, and the partial of type child.Parent that was null, and I didn't get a null reference, I did in fact get the type mismatch error. In fact, even if explicitly passed null on to the view:
通常这些问题会导致OP在其他地方出错,并且错误不可重复。我尝试重新创建你的确切场景,你知道什么?你真的是对的:)我创建了一个简单的父/子关系,并且View的类型是child,而child.Parent类型的部分是null,我没有得到空引用,我实际上做了得到类型不匹配错误。实际上,即使将null显式传递给视图:
Html.RenderPartial("MyPartial",null);
or even with a direct cast:
甚至是直接演员:
Html.RenderPartial("MyPartial",(Parent)null);
it still gave the mismatch error.
它仍然给出了不匹配错误。
My guess and this really is a total guess, is that when it detects that the object being passed in is null, it just defaults to using the Model of the actual view itself. I'm not sure either if this is by design or a bug, but that's the only thing that makes sense.
我猜这真的是一个总猜测,就是当它检测到传入的对象为空时,它只是默认使用实际视图本身的模型。我不确定这是设计还是错误,但这是唯一有意义的事情。
#2
I believe you should avoid passing null Models to your Views. Use default/empty Model if your Model is null. This should work:
我相信你应该避免将null模型传递给你的视图。如果您的Model为null,请使用default / empty Model。这应该工作:
Html.RenderPartial("Address", Model.address ?? new Address { /* OPTIONAL: default values */});
#1
Very often these kinds of questions result in the OP making a mistake elsewhere, and the error isn't reproducible. I tried recreating your exact scenario, and what do you know? You're actually right :) I created a simple Parent / Child relationship and had the View be of type child, and the partial of type child.Parent that was null, and I didn't get a null reference, I did in fact get the type mismatch error. In fact, even if explicitly passed null on to the view:
通常这些问题会导致OP在其他地方出错,并且错误不可重复。我尝试重新创建你的确切场景,你知道什么?你真的是对的:)我创建了一个简单的父/子关系,并且View的类型是child,而child.Parent类型的部分是null,我没有得到空引用,我实际上做了得到类型不匹配错误。实际上,即使将null显式传递给视图:
Html.RenderPartial("MyPartial",null);
or even with a direct cast:
甚至是直接演员:
Html.RenderPartial("MyPartial",(Parent)null);
it still gave the mismatch error.
它仍然给出了不匹配错误。
My guess and this really is a total guess, is that when it detects that the object being passed in is null, it just defaults to using the Model of the actual view itself. I'm not sure either if this is by design or a bug, but that's the only thing that makes sense.
我猜这真的是一个总猜测,就是当它检测到传入的对象为空时,它只是默认使用实际视图本身的模型。我不确定这是设计还是错误,但这是唯一有意义的事情。
#2
I believe you should avoid passing null Models to your Views. Use default/empty Model if your Model is null. This should work:
我相信你应该避免将null模型传递给你的视图。如果您的Model为null,请使用default / empty Model。这应该工作:
Html.RenderPartial("Address", Model.address ?? new Address { /* OPTIONAL: default values */});