如果我有观点继承自:
System.Web.Mvc.ViewPage<Foo>
Foo有一个类型字符串的属性栏@H_404_5@而且视图想要渲染强类型的部分视图,它继承自:
System.Web.Mvc.ViewUserControl<string>
喜欢这个:
Html.RenderPartial("_Bar",Model.Bar);%>
那为什么会这样呢?
The model item passed into the dictionary is of type ‘Foo’@H_404_5@ but this dictionary requires a model item of type ‘System.String’.
当bar未初始化时?
更具体:为什么它通过Foo,它应该通过null?
解决方法
@H_404_27@ 如@Dennis指出的,如果模型值为null,它将使用视图中的现有模型。其原因是支持使用仅包含部分视图名称并使其重用现有模型的签名来调用部分视图的功能。在内部,所有的RenderPartial帮助器都推迟到一个RenderPartialInternal方法。获取该方法以重用现有模型的方式是传递模型的空值(仅使用视图名称的签名)。当您将空值传递给包含视图名称和模型对象的签名时,您基本上会复制仅使用视图名称的方法的行为。这应该解决你的问题:
<% Html.RenderPartial( "_Bar",Model.Bar ?? string.Empty ) %>