2014-10-06 146 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)