【问题标题】:How to get sum of columns from razor commands Asp .net core?如何从剃刀命令 Asp .net 核心中获取列的总和?
【发布时间】: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


【解决方案1】:

使用@,如下所示

@(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>

截图

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-18
    • 2019-06-13
    • 2019-12-13
    • 2020-08-21
    • 2020-08-09
    • 2021-08-07
    相关资源
    最近更新 更多