传递到字典中的模型项的类型为 [英] The model item passed into the dictionary is of type

查看:58
本文介绍了传递到字典中的模型项的类型为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我很容易理解的错误,但是无法找出正确的解决方法.

I have an error that I kind of understand, but can't figure out to solve in the right way.

我有一个母版页,我称该母版页为

I have a MasterPage, from that MasterPage I call:

<% Html.RenderPartial("Tags"); %>

标签是一个强类型的视图,如下所示:

Tags is a strongly typed view that looks like this:

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

我这样返回PartialView:

I return the PartialView like this:

return View("Tags", result);

结果正确且类型为

List<CategoryCount>

错误是这样的:

传递到字典中的模型项的类型为'System.Collections.Generic.List 1 [JegManglerEn.Item]',但是此字典需要类型为'System.Collections.Generic.IEnumerable <的模型项/code>1[JegManglerEn.Controllers.CategoryCount]".

The model item passed into the dictionary is of type 'System.Collections.Generic.List1[JegManglerEn.Item]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[JegManglerEn.Controllers.CategoryCount]'.

因此设置如下:

MasterPage加载一个视图,该视图采用JegManglerEn.Item的集合,并且还尝试加载PartialView,该视图采用JegManglerEn.Controllers.CategoryCount的集合...但是没有这样做.

MasterPage loads a View that takes a collection of JegManglerEn.Item AND also tries to load a PartialView that takes a collection of a JegManglerEn.Controllers.CategoryCount...but fails to do so.

我知道它与RenderPartial方法有关,因为如果我将其更改为RenderAction,则效果很好.

I know it has something to do with the RenderPartial method because if I change it to RenderAction it's works great.

如果你们有答案,我可以不看MVC资源或启动Reflector.

If you guys have the answer I get to skip looking at the MVC soruce or startup Reflector.

谢谢!

推荐答案

调用Html.RenderPartial的单参数重载时,您将隐式地将父视图的整个模型传递给局部视图.这不起作用,因为视图期望的模型类型不匹配.

When you call the single-parameter overload of Html.RenderPartial, you're implicitly passing the entire model of the parent view down to the partial. This doesn't work, as the model types expected by the views do not match.

使RenderPartial在您的上下文中工作的最简单方法是创建一个既包含标签又包含页面模型数据的复合模型类型,然后调用重载,将父模型的一部分传递给局部模型.

The simplest way to get RenderPartial working in your context is to create a composite model type that contains both the Tags and the page model data, then call the overload where you pass a part of the parent model to the partial:

Html.RenderPartial("Tags", Model.Tags);

话虽这么说,我认为RenderAction最有可能在这里使用,因为它可以更轻松地避免在控制器中重复代码(当然,假设您对继承的所有页面都以相同的方式加载标签数据)从相关的母版页开始).

That being said, I think RenderAction is most likely the way to go here, as it makes it easier to avoid duplicate code in your controllers (of course assuming you're loading the tags data in the same way for all pages inheriting from the masterpage in question).

这篇关于传递到字典中的模型项的类型为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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