【问题标题】:Assign a query result to an object inside a controller将查询结果分配给控制器内的对象
【发布时间】: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


【解决方案1】:

首先,您的查询只计算所有成员。加入实际上是毫无意义的。如果您想计算每个俱乐部的成员,您需要一个 group by 子句和 group by 诸如俱乐部 ID 或名称之类的内容。

您发送到视图的模型只是一个IEnumerable&lt;Club&gt;,因此实际上没有任何地方可以添加计数。您要么需要使用视图模型,在其中可以为计数添加属性,要么只需将计数添加到ViewBag 成员。那么,在您看来,您可以在遍历您的俱乐部列表时从中提取特定俱乐部的计数。

【讨论】:

  • 你能给我一个例子如何做到这一点。我是 linq 和 MVC 的新手
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-28
  • 2014-07-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多