C# 计算表中一列中的值之和

C# 计算表中一列中的值之和,c#,asp.net-mvc,C#,Asp.net Mvc,我试图在MVC中添加表中任何数字列的总值,并在标题行中显示总值。例如,我想在TotalCashStack列中添加所有值,并将该数字显示在标题行中。我是MVC的新手,我不知道如何实现这一点 这是index.cshtml页面: @model IEnumerable<DailyReport> @{ ViewBag.Title = "Index"; } <h2>Index</h2> <p> @Html.ActionLink("Crea

我试图在MVC中添加表中任何数字列的总值,并在标题行中显示总值。例如,我想在TotalCashStack列中添加所有值,并将该数字显示在标题行中。我是MVC的新手,我不知道如何实现这一点

这是index.cshtml页面:

@model IEnumerable<DailyReport>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")

    @using (Html.BeginForm())
    {
    <p>
        Start Date: @Html.TextBox("StartDate") <br />
        <br />
        End Date: @Html.TextBox("EndDate") <br />
        <br />
        <input type="submit" value="Filter" />
    </p>
    }



</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.DailyReportDate)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.BettingShop)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Estate)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.SisSrNumber)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ShopBalance)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.TotalCashStake)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.TotalOtherCashOut)
        </th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.DailyReportDate)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.BettingShop)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Estate)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.SisSrNumber)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ShopBalance)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TotalCashStake)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TotalOtherCashOut)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.DailyReportId }) |
            @Html.ActionLink("Details", "Details", new { id=item.DailyReportId }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.DailyReportId })
        </td>
    </tr>
}
</table>

这是我的index.cshtml页面的源代码:

@model IEnumerable<DailyReport>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")

    @using (Html.BeginForm())
    {
    <p>
        Start Date: @Html.TextBox("StartDate") <br />
        <br />
        End Date: @Html.TextBox("EndDate") <br />
        <br />
        <input type="submit" value="Filter" />
    </p>
    }



</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.DailyReportDate)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.BettingShop)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Estate)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.SisSrNumber)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ShopBalance)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.TotalCashStake)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.TotalOtherCashOut)
        </th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.DailyReportDate)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.BettingShop)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Estate)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.SisSrNumber)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ShopBalance)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TotalCashStake)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TotalOtherCashOut)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.DailyReportId }) |
            @Html.ActionLink("Details", "Details", new { id=item.DailyReportId }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.DailyReportId })
        </td>
    </tr>
}
</table>
@model IEnumerable
@{
ViewBag.Title=“Index”;
}
指数

@ActionLink(“新建”、“创建”)
@使用(Html.BeginForm())
{

开始日期:@Html.TextBox(“开始日期”)

结束日期:@Html.TextBox(“结束日期”)

}

@DisplayNameFor(model=>model.DailyReportDate) @DisplayNameFor(model=>model.BettingShop) @DisplayNameFor(model=>model.Estate) @DisplayNameFor(model=>model.SisSrNumber) @DisplayNameFor(model=>model.ShopBalance) @DisplayNameFor(model=>model.totalCashStack) @DisplayNameFor(model=>model.TotalOtherCashOut) @foreach(模型中的var项目){ @DisplayFor(modelItem=>item.DailyReportDate) @DisplayFor(modelItem=>item.BettingShop) @DisplayFor(modelItem=>item.Estate) @DisplayFor(modeleItem=>item.SisSrNumber) @DisplayFor(modelItem=>item.ShopBalance) @DisplayFor(modelItem=>item.TotalCashStack) @DisplayFor(modelItem=>item.TotalOtherCashOut) @ActionLink(“编辑”,“编辑”,新的{id=item.dailryreportid})| @ActionLink(“详细信息”,“详细信息”,新的{id=item.dailryreportid})| @ActionLink(“删除”,“删除”,新的{id=item.DailyReportId}) }
这是我的控制器的代码:

namespace Reporting.Controllers
{
    public class DailyReportController : Controller
    {
        private RiskEntities db = new RiskEntities();

        // GET: /DailyReport/
        public ActionResult Index(DateTime? startDate, DateTime? endDate)
        {


            if (startDate == null || endDate == null)
            {
                return View(db.DailyReports.ToList());
            }
            var endDateToUse = (DateTime) endDate;
            endDateToUse = endDateToUse.AddDays(+1);
            var dailyReports = (from dr in db.DailyReports
                                where dr.DailyReportDate >= startDate
                                && dr.DailyReportDate <= endDateToUse
                                select dr);



            return View(dailyReports.ToList());
        }

        // GET: /DailyReport/Details/5
        public ActionResult Details(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            DailyReport dailyreport = db.DailyReports.Find(id);
            if (dailyreport == null)
            {
                return HttpNotFound();
            }
            return View(dailyreport);
        }

        // GET: /DailyReport/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: /DailyReport/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include="DailyReportId,DailyReportDate,BettingShop,Estate,SisSrNumber,ShopBalance,TotalCashStake,TotalOtherCashOut,TotalPhoneAccountCashDeposit,TotalPhoneAccountCashWithdrawl,TotalCashFromBank,TotalTransfersFromOtherShops,TotalPaidByOtherShops,TotalCashToBank,TotalTransfersToOtherShops,TotalPaidHereForOtherShops,TotalExpenses,TotalOtherCashIn,TotalPayoutCancellations,TotalStakes,TotalStakeLessVoids,TotalVoid,TotalPayouts,TotalCreditPayouts,TotalCreditStakes,TotalCreditVoids,TotalEndFloatActual,PendingPayouts6Days,PendingPayouts1Day,TotalCashIn,TotalCashPayoutIncludingVoids,TotalCashPayoutExcludingVoids,TotalCashPayoutForHere,NumberCashSlips,NumberAccountSlips,NumberCashSlipsPaidForOtherDays,NumberCashSlipsPaidForToday,TotalTillTransferIn,TotalTillTransferOut,TotalCashOut,TotalCashLeftHandSide,TotalCashRightHandSide")] DailyReport dailyreport)
        {
            if (ModelState.IsValid)
            {
                dailyreport.DailyReportId = Guid.NewGuid();
                db.DailyReports.Add(dailyreport);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(dailyreport);
        }

        // GET: /DailyReport/Edit/5
        public ActionResult Edit(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            DailyReport dailyreport = db.DailyReports.Find(id);
            if (dailyreport == null)
            {
                return HttpNotFound();
            }
            return View(dailyreport);
        }

        // POST: /DailyReport/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include="DailyReportId,DailyReportDate,BettingShop,Estate,SisSrNumber,ShopBalance,TotalCashStake,TotalOtherCashOut,TotalPhoneAccountCashDeposit,TotalPhoneAccountCashWithdrawl,TotalCashFromBank,TotalTransfersFromOtherShops,TotalPaidByOtherShops,TotalCashToBank,TotalTransfersToOtherShops,TotalPaidHereForOtherShops,TotalExpenses,TotalOtherCashIn,TotalPayoutCancellations,TotalStakes,TotalStakeLessVoids,TotalVoid,TotalPayouts,TotalCreditPayouts,TotalCreditStakes,TotalCreditVoids,TotalEndFloatActual,PendingPayouts6Days,PendingPayouts1Day,TotalCashIn,TotalCashPayoutIncludingVoids,TotalCashPayoutExcludingVoids,TotalCashPayoutForHere,NumberCashSlips,NumberAccountSlips,NumberCashSlipsPaidForOtherDays,NumberCashSlipsPaidForToday,TotalTillTransferIn,TotalTillTransferOut,TotalCashOut,TotalCashLeftHandSide,TotalCashRightHandSide")] DailyReport dailyreport)
        {
            if (ModelState.IsValid)
            {
                db.Entry(dailyreport).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(dailyreport);
        }

        // GET: /DailyReport/Delete/5
        public ActionResult Delete(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            DailyReport dailyreport = db.DailyReports.Find(id);
            if (dailyreport == null)
            {
                return HttpNotFound();
            }
            return View(dailyreport);
        }

        // POST: /DailyReport/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(Guid id)
        {
            DailyReport dailyreport = db.DailyReports.Find(id);
            db.DailyReports.Remove(dailyreport);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
命名空间报告。控制器
{
公共类DailyReportController:控制器
{
私有风险实体db=新的风险实体();
//获取:/dailreport/
公共行动结果索引(DateTime?startDate,DateTime?endDate)
{
if(startDate==null | | endDate==null)
{
返回视图(db.DailyReports.ToList());
}
var endDateToUse=(DateTime)endDate;
endDateToUse=endDateToUse.AddDays(+1);
var dailyReports=(来自db.dailyReports中的dr
其中,dr.DailReportDate>=开始日期

&&dr.DailyReportDate我建议您在控制器中计算总和,然后扩展模型,将该值单独包含到表数据中。这样,视图只显示从服务器发送的数据,而不是动态计算值。这也将提高页面的性能。

我建议在jQuery中使用DataTables | Table插件,这是一个开源插件,或者类似的东西,而不是为每个控制器重新发明轮子。

这将适用于所有类型的表格数据

您可以在此处看到它们是如何合计的:


这还将为您带来其他好处,如分页和开箱排序。

请指定您的意思。您是想对某列在DB中的每个值求和,还是只想将控制器发送的数据相加?您可以使用简单的lambda扩展方法
Model.sum(item=>item.totalcashstack)
(只是提示,我不熟悉asp)只需将控制器发送的数据相加,例如TotalCashStack列的值,并将该值显示在标题行中。请不要盲目复制/粘贴整个文档。如果您将其限制为相关代码,人们会更倾向于帮助您。越多越好:)作为关于这个问题的更一般的提示:
int total=MyObjects.Select(myObj=>myObj.SomeNumber)
。传递计算值最好通过ViewBag或ViewModel完成。两者都可以,但最好在同一应用程序中保持一致。