【发布时间】:2018-01-18 10:57:24
【问题描述】:
我搜索了这个解决方案,但没有找到我正在寻找的确切内容。我在 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>
我的目标是将计数变量的结果放入俱乐部中并返回,以便我可以在俱乐部视图中将其显示为“TotalMembers”
【问题讨论】:
-
这里有什么问题?你有什么异常吗?
-
我希望将 count 变量的结果放在俱乐部内并返回,这样我就可以在俱乐部视图中将其显示为 "TotalMembers" 。没有执行,因为我还没有使用“count”变量。
-
在
.ToList().Count();这一行使用Count()时,您不必使用ToList()。仅使用Count()。您还需要通过运行for循环在该模型内设置计数。
标签: c# entity-framework linq asp.net-core-mvc