如何在Credits列中显示学生入伍的所有课程的学分数。
我需要修改Contoso大学的应用程序。
查看:
@model PagedList.IPagedList<ContosoUniversity.Models.Student>
@using PagedList.Mvc;
<h2>Students</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
@using (Html.BeginForm("Index", "Student", FormMethod.Get))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<p>
Find by name: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
<input type="submit" value="Filter" />
</p>
}
<table class="table">
<tr>
<th>
@Html.ActionLink("Last Name", "Index", new { sortOrder = ViewBag.NameSortParm })
</th>
<th>
First Name
</th>
<th>
@Html.ActionLink("Enrollment Date", "Index", new { sortOrder = ViewBag.DateSortParm })
</th>
<th>
Date Of Birth
</th>
<th>
Courses
</th>
<th>
Credits
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.FirstMidName)
</td>
<td>
@Html.DisplayFor(modelItem => item.EnrollmentDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.DateOfBirth)
</td>
<td>
@string.Join(",", item.Enrollments.Select(e => e.Course.Title))
</td>
<td>
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}
</table>
<br />
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
@Html.PagedListPager(Model, page => Url.Action("Index",
new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))
请帮助!
我不明白该怎么做!
如果有人需要更多详情,请告诉我。
答案 0 :(得分:2)
我对Contoso大学没有任何经验,但在我看来,你可以通过做这样的事情来总结学分:
item.Enrollments.Sum(e => e.Course.Credits);
这是未经测试的,如果不知道数据的结构如何,很难确定这是否可行,但我确信Sum()方法可以帮助您。
答案 1 :(得分:1)
@ item.Enrollments.Sum(e =&gt; e.Course.Credits)