【发布时间】:2010-11-09 09:24:51
【问题描述】:
背景
我在尝试在 ASP.NET MVC 中呈现部分视图时收到以下错误。我是 ASP.NET MVC 的新手,我确信这个错误很容易解决,只是因为我缺乏完全的理解。
问题(对于那些不想阅读所有内容的人):
是什么导致了这个错误?
异常详情:
System.InvalidOperationException: 传入的模型项 字典是类型'MyApp.Models.ClassroomFormViewModel'但是这本词典需要一个模型 类型的项目'System.Collections.Generic.IEnumerable1[MyApp.Models.ClassroomFormViewModel]'`。
实体
我有两个具有父/子关系的实体。
课堂便笺 ------------ ----------- 编号 1 ----- 编号 姓名\姓名 (...) \ 内容 ---- * 教室ID型号
在Model 中,StickyNote 内容保存在不同的表中,并且可以通过以下方法访问(使用Linq-to-SQL:
public IQueryable<StickyNote> GetStickyNotesByClassroom(Classroom classroom)
{
return from stickynote in db.StickyNotes
where stickynote.ClassroomID == classroom.ID
select stickynote;
}
错误
我创建了一个局部视图来显示 StickyNote 内容,因为它“属于”它所在的教室。我遇到的问题是我无法让它显示,并收到以下错误:
传入的模型项 字典的类型:
'MyApp.Models.ClassroomFormViewModel'但是这本词典需要一个模型 类型的项目'System.Collections.Generic.IEnumerable1[MyApp.Models.ClassroomFormViewModel]'`。 描述:未处理的异常 在执行过程中发生 当前的网络请求。请查看 堆栈跟踪以获取有关的更多信息 错误及其起源 代码。异常详情:
System.InvalidOperationException: 传入的模型项 字典是类型'MyApp.Models.ClassroomFormViewModel'但是这本词典需要一个模型 类型的项目'System.Collections.Generic.IEnumerable1[MyApp.Models.ClassroomFormViewModel]'`。
局部视图
这里是部分视图代码:
<%@ Control Language="C#" Inherits="
System.Web.Mvc.ViewUserControl<IEnumerable<MyApp.Models.ClassroomFormViewModel>>" %>
<table background="../../images/corkboard.jpg">
<% foreach (var items in Model) { %>
<tr>
<% foreach (var item in items.StickyNotes) { %>
<td><div class="sticky_note_container">
<!-- actually use a post it note here on the page -->
<div class="sticky_note">
<div class="sticky_note_content">
<!-- content of sticky note here -->
<% Html.ActionLink(item.Name, "ShowStickyNoteContent"); %>
<!-- end of content of sticky note -->
</div>
</div>
<div class="sticky_note_footer"> </div>
<br clear="all" />
</div>
</td>
<% } %>
</tr>
<% } %>
</table>
父视图
以及调用它的另一个视图中的代码:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits=
"System.Web.Mvc.ViewPage<MyApp.Models.ClassroomFormViewModel>" %>
{...}
<%
Html.RenderPartial("StickyNotes", Model);
%>
【问题讨论】:
标签: c# .net asp.net-mvc view partial-views