【发布时间】:2021-06-24 00:57:05
【问题描述】:
这是模型
我想在 Mon,Tue,Wed,Thr,Fri,Sat,Sun 中获取值的总和并将它们显示在不同的页面上。 使用 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;
}
【问题讨论】:
-
为什么不在模型上创建一个只读属性来返回它们的总和? public int DaysSum => Mon + Tue + Wed + Thr + Fri + Sat + Sun;然后,您可以在拥有模型的任何地方使用该属性。
-
嗯,好的,谢谢,我试试。 @RyanThomas 我是这门语言的初学者。
-
我也想得到单个列的总数
-
啊,所以你有一个 List
并且想要总共说“Mon”?
标签: c# asp.net sql-server asp.net-mvc asp.net-core-webapi