c# - 如何从剃须刀命令 Asp .net 核心中获取列的总和?
问题描述
这是模型
我想获取周一、周二、周三、周四、周五、周六、周日的值总和,并将它们显示在不同的页面上。使用add migrations命令将数据库与本地数据库连接
namespace TimeSheat.Models
{
public class Timesheet
{
public int Id { get; set; }
public int UserID { get; set; }
public string Project { get; set; }
public string Activity { get; set; }
public string DeptCode { get; set; }
public int Mon { get; set; }
public int Tue { get; set; }
public int Wed { get; set; }
public int Thr { get; set; }
public int Fri { get; set; }
public int Sat { get; set; }
public int Sun { get; set; }
public Timesheet()
{
}
}
}
如果需要,这些是一些控制器方法
public async Task<IActionResult> ShowSearch()
{
return View(await _context.Timesheet.ToListAsync());
}
// GET: Timesheets/ShowSearchResults
public async Task<IActionResult> ShowSearchResults(string SearchPhrase)
{
return View("Index", await _context.Timesheet.Where(j => j.Project.Contains(SearchPhrase)).ToListAsync());
}
// GET: Timesheets/ShowSummary
public async Task<IActionResult> ShowSummary()
{
return View(await _context.Timesheet.ToListAsync());
}
// GET: Timesheets/ShowSummary
public async Task<IActionResult> ShowSummaryResults(int SearchPhrase)
{
var a = View("Index", await _context.Timesheet.Where(j => j.UserID.Equals(SearchPhrase)).ToListAsync());
return a;
}
解决方案
使用@
如下
@(item.Mon + item.Tue + item.Wed + item.Thr + item.Fri + item.Sat + item.Sun)
示例视图代码
@model IList<TimeSheat.Models.Timesheet>
@{
ViewData["Title"] = "Summary";
}
<h1>Summary</h1>
<table class="table">
<thead>
<tr>
<td>Id</td>
<td>Mon</td>
<td>Tue</td>
<td>Wed</td>
<td>Thr</td>
<td>Fri</td>
<td>Sat</td>
<td>Sun</td>
<td>
Total
</td>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Id)
</td>
<td>
@Html.DisplayFor(modelItem => item.Mon)
</td>
<td>
@Html.DisplayFor(modelItem => item.Tue)
</td>
<td>
@Html.DisplayFor(modelItem => item.Wed)
</td>
<td>
@Html.DisplayFor(modelItem => item.Thr)
</td>
<td>
@Html.DisplayFor(modelItem => item.Fri)
</td>
<td>
@Html.DisplayFor(modelItem => item.Sat)
</td>
<td>
@Html.DisplayFor(modelItem => item.Sun)
</td>
<td>
@(item.Mon + item.Tue + item.Wed + item.Thr + item.Fri + item.Sat + item.Sun)
</td>
</tr>
}
</tbody>
</table>
截屏
推荐阅读
- android - 在 RelativeLayout 上未检测到滑动
- java - 如何在 Bazel BUILD 中使用 gradle 的 apply 插件?
- patch - “不支持 git 二进制差异”错误使用 yocto
- angular - 错误 TS2339:“FormData”类型上不存在属性“条目”
- java - 使用自己的密码和数字签名算法
- java - 网络故障后回滚 Spring Transaction
- kubernetes - Kubernetes - 使用自定义更改原因更新部署
- php - 您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以在第 1 行附近使用正确的语法
- swagger - 大摇大摆的编辑器:无法使用 $ref 添加本地引用
- blender - Blender AL lib:UpdateDeviceParams:无法设置 44100hz,得到 48000hz,而不是 Windows