SfondoLavorare con vista parziale in ASP.NET MVC
Ricevo il seguente errore quando si cerca di rendere una vista parziale in ASP.NET MVC. Sono nuovo di ASP.NET MVC e sono sicuro che l'errore è semplice da risolvere e deriva dalla mia mancanza di comprensione completa.
Domanda (per chi non vuole leggere tutto):
che cosa sta causando questo errore?
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]'`.
entités
Ho due entità con una relazione padre/figlio.
Classroom StickyNote ------------ ----------- Id 1 ----- Id Name \ Name (...) \ Content ---- * ClassroomID
Modello
Nel Model
il Contenuto StickyNote è tenuto in una tabella diversa, e vi si accede (tramite Linq-to-SQL
attraverso il seguente metodo:
public IQueryable<StickyNote> GetStickyNotesByClassroom(Classroom classroom)
{
return from stickynote in db.StickyNotes
where stickynote.ClassroomID == classroom.ID
select stickynote;
}
errore
Ho creato una vista parziale f o visualizzando il contenuto di StickyNote
poiché "appartiene" alla classe in cui si trova. Il problema che sto funzionando in è che io non sono in grado di farlo per visualizzare, e visualizzato il seguente errore:
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]'`.
Vista parziale
Ecco il codice vista parziale:
<%@ 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 Visualizza
E il codice dall'altro View che chiama IT:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits=
"System.Web.Mvc.ViewPage<MyApp.Models.ClassroomFormViewModel>" %>
{...}
<%
Html.RenderPartial("StickyNotes", Model);
%>