asp.net-mvc - 具有空模型的 renderpartial 传递了错误的类型

标签 asp.net-mvc 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 不为 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/

相关文章:

asp.net-mvc - 为什么 Asp.net 部分 View 不返回?

网络 MVC : SSO + AD group?

.net - mvc路由问题——使用整型参数

javascript - 将消息从 Controller 传递到 View 中的 AJAX 脚本

javascript - 如何在不在 ReactJS 中重新渲染整个树的情况下渲染树组件的叶组件

asp.net-mvc - 使用模型和 ViewData 项创建 ViewDataDictionary 的简写?

jquery-ui - Dotnetnuke 部分渲染使我的 jQueryUI 小部件停止工作

c# - 如何使用 paypal 直接付款 NPV API c# 设置自定义定期付款服务

c# - 模拟 ASP.NET 身份验证 cookie

asp.net-mvc - ASP.NET MVC中的ModelFactory解决 'RenderPartial'问题