我需要在單個視圖index.cshtml中顯示來自兩個表(學生和成績)的數據。使用部分視圖在單個視圖中顯示多個模型
我有兩個部分視圖_StudentPartial & _GradePartial都強烈類型。我搜索了一下,所有人都說我應該使用父母模型。所以我在下面創建了一個名爲MyViewModels的父模型,但我似乎無法得到它的工作。什麼是正確的方法來做到這一點?
我的模型:
public class MyBigViewModels{
public IEnumerable<Users.Models.Student> Student { get; set; }
public IEnumerable<Users.Models.Grade> Grade { get; set; }
}
我的視圖():
@model MyApp.Models.MyBigViewModels
// render content for Student
@foreach (var item in Model)
{
@Html.Partial("_StudentPartial", item)
}
// Render content for Grades
@foreach (var item in Model)
{
@Html.Partial("_GradePartial", item)
}
我的部分景色
// _StudentPartial
@model IEnumerable<MyApp.Models.Student>
@foreach (var item in Model) {
@Html.DisplayFor(modelItem => item.name)
}
// _GradePartial
@model IEnumerable<MyApp.Models.Grade>
@foreach (var item in Model) {
@Html.DisplayFor(modelItem => item.letterGrade)
}
服務器錯誤 '/' 應用
傳入字典的模型項目類型爲 'System.Data.Entity.Infrastructure.DbQuery
1[MyApp.Models.Students]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1 [MyApp.Models.MyBigViewModels]'。