Background
背景
I'm receiving the following error when trying to render a partial view in ASP.NET MVC. I am new to ASP.NET MVC and am sure the error is simple to resolve and just stems from my lack of complete understanding.
尝试在ASP.NET MVC中呈现部分视图时,我收到以下错误。我是ASP.NET MVC的新手,我确信错误很容易解决,而且源于我缺乏完整的理解。
Question (for those not wanting to read everything):
问题(对于那些不想阅读所有内容的人):
What is causing this error ?
导致此错误的原因是什么?
Exception Details:
System.InvalidOperationException
: The model item passed into the dictionary is of type'MyApp.Models.ClassroomFormViewModel'
but this dictionary requires a model item of type'System.Collections.Generic.IEnumerable
1[MyApp.Models.ClassroomFormViewModel]'`.异常详细信息:System.InvalidOperationException:传递到字典中的模型项的类型为“MyApp.Models.ClassroomFormViewModel”,但此字典需要类型为“System.Collections.Generic.IEnumerable1 [MyApp.Models.ClassroomFormViewModel]”的模型项。 。
Entites
的entites
I have two entities with a parent/child relationship.
我有两个父/子关系的实体。
Classroom StickyNote ------------ ----------- Id 1 ----- Id Name \ Name (...) \ Content ---- * ClassroomID
Model
模型
In the Model
the StickyNote Content is kept in a different table, and accessed (using Linq-to-SQL
through the following method:
在模型中,StickyNote内容保存在不同的表中,并进行访问(使用Linq-to-SQL通过以下方法:
public IQueryable<StickyNote> GetStickyNotesByClassroom(Classroom classroom)
{
return from stickynote in db.StickyNotes
where stickynote.ClassroomID == classroom.ID
select stickynote;
}
Error
错误
I've created a partial view for displaying StickyNote
content since it 'belongs' to the classroom it's on. The problem I'm running into is that I'm not able to get it to display, and receive the following error:
我创建了一个部分视图来显示StickyNote内容,因为它“属于”它所在的教室。我遇到的问题是我无法显示它,并收到以下错误:
The model item passed into the dictionary is of type:
'MyApp.Models.ClassroomFormViewModel'
but this dictionary requires a model item of type'System.Collections.Generic.IEnumerable
1[MyApp.Models.ClassroomFormViewModel]'`. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.传递到字典中的模型项的类型为:'MyApp.Models.ClassroomFormViewModel',但此字典需要类型为'System.Collections.Generic.IEnumerable1 [MyApp.Models.ClassroomFormViewModel]'的模型项。描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
Exception Details:
System.InvalidOperationException
: The model item passed into the dictionary is of type'MyApp.Models.ClassroomFormViewModel'
but this dictionary requires a model item of type'System.Collections.Generic.IEnumerable
1[MyApp.Models.ClassroomFormViewModel]'`.异常详细信息:System.InvalidOperationException:传递到字典中的模型项的类型为“MyApp.Models.ClassroomFormViewModel”,但此字典需要类型为“System.Collections.Generic.IEnumerable1 [MyApp.Models.ClassroomFormViewModel]”的模型项。 。
Partial View
部分视图
Here is the partial view code:
这是局部视图代码:
<%@ 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>
Parent View
父视图
And the code from the other View that calls it:
来自另一个View的代码调用它:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits=
"System.Web.Mvc.ViewPage<MyApp.Models.ClassroomFormViewModel>" %>
{...}
<%
Html.RenderPartial("StickyNotes", Model);
%>
2 个解决方案
#1
8
You are passing in a single instance of your ClassroomFormViewModel into and the View is expecting a a collection i.e. IEnumerable<ClassroomFormViewModel>.
您正在将ClassroomFormViewModel的单个实例传入,并且View期望一个集合,即IEnumerable
Change your declaration in your PartialView to
将PartialView中的声明更改为
Inherits="
System.Web.Mvc.ViewUserControl<MyApp.Models.ClassroomFormViewModel>"
OR
要么
What you really want (after really looking at your code) IS an IEnumerable<ClassroomFormViewModel>
你真正想要的(在真正查看你的代码之后)是一个IEnumerable
so your Model in your calling page needs to be of IEnumerable<ClassroomFormViewModel>
所以你的调用页面中的模型需要是IEnumerable
Essentially you're trying to do this
基本上你正在努力做到这一点
public void Render(ClassroomFormViewModel model)
{
RenderPartial(model) //Cannot cast single instance into an IEnumerable
}
public string RenderPartial(IEnumerable<ClassroomFormViewModel> model)
{
//Do something
}
#2
2
Your partial should start
你的部分应该开始
<%@ Control Language="C#" Inherits="
System.Web.Mvc.ViewUserControl<MyApp.Models.ClassroomFormViewModel>" >
I guess you want to display one classroom on you page. If you want to display more then dont use a list of viewmodels. Use one viewmodel that has a list of classrooms
我想你想在你的页面上显示一个教室。如果要显示更多,请不要使用视图模型列表。使用一个包含教室列表的视图模型
#1
8
You are passing in a single instance of your ClassroomFormViewModel into and the View is expecting a a collection i.e. IEnumerable<ClassroomFormViewModel>.
您正在将ClassroomFormViewModel的单个实例传入,并且View期望一个集合,即IEnumerable
Change your declaration in your PartialView to
将PartialView中的声明更改为
Inherits="
System.Web.Mvc.ViewUserControl<MyApp.Models.ClassroomFormViewModel>"
OR
要么
What you really want (after really looking at your code) IS an IEnumerable<ClassroomFormViewModel>
你真正想要的(在真正查看你的代码之后)是一个IEnumerable
so your Model in your calling page needs to be of IEnumerable<ClassroomFormViewModel>
所以你的调用页面中的模型需要是IEnumerable
Essentially you're trying to do this
基本上你正在努力做到这一点
public void Render(ClassroomFormViewModel model)
{
RenderPartial(model) //Cannot cast single instance into an IEnumerable
}
public string RenderPartial(IEnumerable<ClassroomFormViewModel> model)
{
//Do something
}
#2
2
Your partial should start
你的部分应该开始
<%@ Control Language="C#" Inherits="
System.Web.Mvc.ViewUserControl<MyApp.Models.ClassroomFormViewModel>" >
I guess you want to display one classroom on you page. If you want to display more then dont use a list of viewmodels. Use one viewmodel that has a list of classrooms
我想你想在你的页面上显示一个教室。如果要显示更多,请不要使用视图模型列表。使用一个包含教室列表的视图模型