出现上述错误的原因是给的List未定义或未找到
在学习ASP.Net Core Razor Page时,使用了ViewModel,按照mvc的逻辑,更改了View的model类型,
系统报上述错误,是自己理解错误,在Razor Page中model应该为IndexModel之类的。自己逻辑有问题;
@page
@model ContosoUniversity.Pages.Courses.IndexModel
//这里应该是IndexModel,后面使用model.CourseVM,访问自定义的视图模型
//这里误写为 List<ContosoUniversity.Model.CourseVM,出现上述错误
@{
ViewData["Title"] = "Courses";
}
<h1>Courses</h1>
<p>
<a asp-page="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.CourseVM[0].CourseId)
</th>
<th>
@Html.DisplayNameFor(model => model.CourseVM[0].Title)
</th>
<th>
@Html.DisplayNameFor(model => model.CourseVM[0].Credits)
</th>
<th>
@Html.DisplayNameFor(model => model.CourseVM[0].DepartmentName)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.CourseVM)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.CourseId)
</td>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.Credits)
</td>
<td>
@Html.DisplayFor(modelItem => item.DepartmentName)
</td>
<td>
<a asp-page="./Edit" asp-route-id="@item.CourseId">Edit</a> |
<a asp-page="./Details" asp-route-id="@item.CourseId">Details</a> |
<a asp-page="./Delete" asp-route-id="@item.CourseId">Delete</a>
</td>
</tr>
}
</tbody>
</table>