2014-10-06 151 views
1

如何在学分列中显示学生征集的所有课程的学分数。在学分列中,显示学生征集的所有课程的学分数

我需要修改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 })) 

请帮助!

我不明白该怎么做!

如果有人需要更多的细节,请告诉我。

回答

2

我没有与Contoso大学的经验,但在我看来,你可能能够做这样的事情,总结学分:

item.Enrollments.Sum(e => e.Course.Credits); 

这是未经测试,很难知道肯定如果这将工作而不知道数据的结构如何,但我相信看看Sum()方法将帮助你。

+0

“ContosoUniversity.Models.Student”不包含关于“萨姆”和没有扩展方法的定义“萨姆”接受第一个参数可以找到'ContosoUniversity.Models.Student'类型(你是否缺少使用指令或程序集引用?) – Romancevic 2014-10-06 18:29:54

+0

当然不是! :)更新了我的代码,以免出现错误。 – Erik 2014-10-06 18:33:24

+2

你是对的!谢谢! – Romancevic 2014-10-06 18:37:49

1

@ item.Enrollments.Sum(E => e.Course.Credits)