我搜索了这个解决方案,但还没找到我正在寻找的东西。我在Visual Studio 2017上使用代码第一实体框架MVC,并有一个Clubs表,Student表和一个链接Members表。每个俱乐部都有许多成员。每个学生都可以成为众多俱乐部的成员。我想要做的是计算与俱乐部控制器内每个俱乐部相关的学生人数。我在Clubs表中有一个字段TotalMembers来保存这个值。我想在俱乐部视图中显示结果。到目前为止这是我的代码。欢迎任何帮助。
我的俱乐部控制器
public async Task<IActionResult> Index(string searchString)
{
//count how many members each club has
var count = (from m in _context.Members
join c in _context.Clubs
on m.ClubID equals c.Id
select m.StudentID).ToList().Count();
ViewData["CurrentFilter"] = searchString;
var clubs = from c in _context.Clubs
select c;
if (!String.IsNullOrEmpty(searchString))
{
clubs = clubs.Where(c => c.Name.Contains(searchString));
}
return View(await clubs.AsNoTracking().ToListAsync());
}
我的俱乐部index.cshtml
@model IEnumerable<ClubsAndSocieties.Models.Club>
@{
ViewData["Title"] = "Index";
}
<h2 class="text-center pageHeading">Current Clubs and Societies</h2>
<form asp-action="Index" method="get">
<div class="form-actions no-color">
<p>
<a asp-action="Create" id="addClass" class="btn btn-info btn-md">
<span class="glyphicon glyphicon-plus"></span> Add
</a>
<input type="text" placeholder=" Find by name:" name="SearchString" value="@ViewData["currentFilter"]" />
<input type="submit" value="Search" class="btn btn-group-sm" />
<a asp-action="Index" class="btn btn-info btn-md">
<span class="glyphicon glyphicon-refresh"></span> Full List
</a>
</p>
</div>
</form>
<div style="overflow-x:auto;">
<table class="responstable">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Chairperson)
</th>
<th>
@Html.DisplayNameFor(model => model.Secretary)
</th>
<th>
@Html.DisplayNameFor(model => model.Treasurer)
</th>
<th>
@Html.DisplayNameFor(model => model.Phone)
</th>
<th>
@Html.DisplayNameFor(model => model.Email)
</th>
<th>
@Html.DisplayNameFor(model => model.TotalMembers)
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Chairperson)
</td>
<td>
@Html.DisplayFor(modelItem => item.Secretary)
</td>
<td>
@Html.DisplayFor(modelItem => item.Treasurer)
</td>
<td>
@Html.DisplayFor(modelItem => item.Phone)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.DisplayFor(modelItem => item.TotalMembers)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.Id" class="btn btn-info btn-md edit"><span class="glyphicon glyphicon-edit"></span> Edit</a>
<a asp-action="Details" asp-route-id="@item.Id" class="btn btn-info btn-md details"><span class="glyphicon glyphicon-info-sign"></span> Details</a>
<a asp-action="Delete" asp-route-id="@item.Id" class="btn btn-info btn-md delete"><span class="glyphicon glyphicon-trash"></span> Delete</a>
</td>
</tr>
}
</tbody>
</table>
</div>
我的目标是将计数变量的结果放在球杆内并返回,这样我就可以在球杆视图中显示为&#34; TotalMembers&#34;
答案 0 :(得分:1)
嗯,首先,您的查询只计算所有成员。加入实际上是毫无意义的。如果你想要计算每个俱乐部的成员数量,你需要一个group by子句和group by by club club或name。
您发送到视图的模型只是IEnumerable<Club>
,因此实际上没有任何地方可以添加计数。您可能需要使用视图模型,您可以在其中添加计数属性,或者只是将计数添加到ViewBag
成员。在你看来,当你迭代你的俱乐部列表时,你可以从中抽出特定俱乐部的数量。