2009-07-09 10 views
10

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">&nbsp;</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); 
    %> 

Odpowiedz

8

Przechodzisz w jednej instancji klasy ClassroomFormViewModel, a widok oczekuje kolekcji, np.IEnumerable<ClassroomFormViewModel>.

Zmień swoją deklarację w PartialView do

Inherits=" 
System.Web.Mvc.ViewUserControl<MyApp.Models.ClassroomFormViewModel>" 

LUB

Co naprawdę chcesz (po naprawdę patrząc na kodzie) jest IEnumerable<ClassroomFormViewModel>

więc swój model na stronie wywołującego musi być IEnumerable<ClassroomFormViewModel>

Zasadniczo próbujesz to zrobić

public void Render(ClassroomFormViewModel model) 
{ 
    RenderPartial(model) //Cannot cast single instance into an IEnumerable 
} 
public string RenderPartial(IEnumerable<ClassroomFormViewModel> model) 
{ 
    //Do something 
} 
2

Twój częściowy powinien zacząć

<%@ Control Language="C#" Inherits=" 
System.Web.Mvc.ViewUserControl<MyApp.Models.ClassroomFormViewModel>" > 

Chyba chcesz wyświetlić jedną klasę na ciebie stronie. Jeśli chcesz wyświetlić więcej, nie używaj listy modeli wyświetlania. Użyj jednego modelu wyświetlania, który ma listę klas szkolnych

Powiązane problemy