首页 > 解决方案 > 如何从剃须刀命令 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;
        }

标签: c#asp.netsql-serverasp.net-mvcasp.net-core-webapi

解决方案


使用@如下

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

截屏

在此处输入图像描述


推荐阅读