与空模型的RenderPartial被传递了错误的类型 [英] renderpartial with null model gets passed the wrong type

查看:118
本文介绍了与空模型的RenderPartial被传递了错误的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页:

<%@ Page Inherits="System.Web.Mvc.View<DTOSearchResults>" %>

和其上,以下内容:

<% Html.RenderPartial("TaskList", Model.Tasks); %>

下面是DTO对象:

public class DTOSearchResults
{
    public string SearchTerm { get; set; }
    public IEnumerable<Task> Tasks { get; set; }

和下面是部分:

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

在Model.Tasks不为空,一切工作正常。然而,当其空我得到:

When Model.Tasks is not null, everything works fine. However when its null I get:

传递到字典中的模型产品类型
  DTOSearchResults,但本词典需要类型的模型项目
  System.Collections.Generic.IEnumerable`1 [任务]'。

The model item passed into the dictionary is of type 'DTOSearchResults' but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Task]'.

我想通它必须不知道哪超载使用,所以我这样做(见下文)是明确的,但我仍然得到同样的问题!

I figured it must not know which overload to use, so I did this (see below) to be explicit, but I still get the same issue!

<% Html.RenderPartial("TaskList", (object)Model.Tasks, null); %>

我知道我可以解决此通过检查空,甚至不及格空,但是这不是重点。为什么会出现这种情况?

I know I can work around this by checking for null, or not even passing null, but that's not the point. Why is this happening?

推荐答案

安德鲁,我认为你所得到的问题是使用电话(视图)的模型,当你通过模型的局部视图中的RenderPartial方法的结果为空..你可以这样做解决这个奇怪的行为:

Andrew I think the problem you are getting is a result of the RenderPartial method using the calling (view)'s model to the partial view when the model you pass is null.. you can get around this odd behavior by doing:

<% Html.RenderPartial("TaskList", Model.Tasks, new ViewDataDictionary()); %>

帮助吗?

这篇关于与空模型的RenderPartial被传递了错误的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆