【发布时间】:2017-01-13 19:19:55
【问题描述】:
这是我的第一个 ASP.NET Core 项目。我有一个单页应用程序,其中在 Index.cshtml 页面中有一个搜索框和一个按钮。如果搜索框文本为空,则不显示任何内容,否则将检索与 pId 对应的记录并显示在 Index.cshtml 页面中。到目前为止,这很好用。
我希望通过聚合给定日期的重量并将其显示在顶部来扩展此视图。因此,一旦用户输入 PId 并提交页面,我想显示
pId total_weight create_dt
在它下面,将是我已经拥有的当前视图
pId bIdc rule_id weight create_dt
我不确定应该在哪里汇总分数?如何更新当前视图以显示聚合值?
控制器
public IActionResult Index(string pId)
{
var scores = from ts in _context.Scores
select ts;
if (!string.IsNullOrEmpty(pId))
{
scores = scores.Where(p => p.p_id.Equals(pId)).OrderByDescending(p => p.create_dt);
return View(scores.ToList());
}
else
return View();
}
Index.cshtml
<div id="p-form">
<form asp-controller="Score" asp-action="Index" method="get">
<p>
<b> P Id:</b> <input type="text" name="pId">
<input type="submit" value="Submit" />
</p>
</form>
</div>
@if (Model != null)
{
<div id="p-scores">
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.p_id)
</th>
<th>
@Html.DisplayNameFor(model => model.b_idc)
</th>
<th>
@Html.DisplayNameFor(model => model.rule_id)
</th>
<th>
@Html.DisplayNameFor(model => model.weight)
</th>
<th>
@Html.DisplayNameFor(model => model.create_dt)
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(model => item.p_id)
</td>
<td>
@Html.DisplayFor(model => item.b_idc)
</td>
<td>
@Html.DisplayFor(model => item.rule_id)
</td>
<td>
@Html.DisplayFor(model => item.weight)
</td>
<td>
@Html.DisplayFor(model => item.create_dt)
</td>
</tr>
}
</tbody>
</table>
</div>
}
【问题讨论】:
-
你试过
GroupBy方法吗? -
我应该在哪里使用 groupby?在控制器中以及如何将它返回到视图中?
标签: c# razor asp.net-core asp.net-core-mvc