TłoPraca z częściowym widokiem w ASP.NET MVC
Otrzymuję następujący błąd podczas próby renderować częściowego widoku w ASP.NET MVC. Jestem nowy w ASP.NET MVC i jestem pewien, że błąd jest prosty do rozwiązania i wynika po prostu z mojego braku pełnego zrozumienia.
Pytanie (dla tych, którzy nie chcą czytać wszystko):
co jest przyczyną tego błędu?
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]'`.
udziałów w jednostkach
Mam dwa podmioty z relacji rodzic/dziecko.
Classroom StickyNote ------------ ----------- Id 1 ----- Id Name \ Name (...) \ Content ---- * ClassroomID
modelu
W Model
Content StickyNote są przechowywane w innej tabeli i dostępne (z użyciem Linq-to-SQL
następującym sposobem:
public IQueryable<StickyNote> GetStickyNotesByClassroom(Classroom classroom)
{
return from stickynote in db.StickyNotes
where stickynote.ClassroomID == classroom.ID
select stickynote;
}
błędów
Utworzyłem częściowy widok f lub wyświetlanie treści od "należącej" do klasy, w której się znajduje. Problem biegnę na to, że nie jestem w stanie zmusić go do wyświetlania, a otrzymasz następujący błąd:
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]'`.
Częściowy widok
Oto częściowy widok Kod:
<%@ 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>
nadrzędna Zobacz
I kod z innego widoku, który wywołuje to:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits=
"System.Web.Mvc.ViewPage<MyApp.Models.ClassroomFormViewModel>" %>
{...}
<%
Html.RenderPartial("StickyNotes", Model);
%>