我有一个页面:
<%@ 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 不为 null 时,一切正常。然而,当它为空时,我得到:
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]'.
我认为它一定不知道要使用哪个重载,所以我这样做(见下文)是为了明确,但我仍然遇到同样的问题!
<% Html.RenderPartial("TaskList", (object)Model.Tasks, null); %>
我知道我可以通过检查 null 来解决这个问题,或者甚至不传递 null,但这不是重点。为什么会发生这种情况?
最佳答案
安德鲁,我认为您遇到的问题是当您传递的模型为空时, RenderPartial 方法使用调用( View )模型到部分 View 的结果。您可以通过执行以下操作来解决这种奇怪的行为:
<% Html.RenderPartial("TaskList", Model.Tasks, new ViewDataDictionary()); %>
这有帮助吗?
关于asp.net-mvc - 具有空模型的 renderpartial 传递了错误的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/650393/