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.
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]'`.
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:
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:
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.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]'`.
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:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits=
"System.Web.Mvc.ViewPage<MyApp.Models.ClassroomFormViewModel>" %>
{...}
<%
Html.RenderPartial("StickyNotes", Model);
%>