C# 如何在asp.net mvc中合计数据库的一列并通过标签显示它?

C# 如何在asp.net mvc中合计数据库的一列并通过标签显示它?,c#,asp.net,asp.net-mvc,razor,view,C#,Asp.net,Asp.net Mvc,Razor,View,我想计算db列的总数(数量),并将其显示在视图中total标签旁边的标签上。我不确定如何执行此特定任务,是否应该使用Javascript完成?或者我可以使用什么其他方法? 这是我的项目 型号 using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Web; namespace webassig

我想计算db列的总数(数量),并将其显示在视图中total标签旁边的标签上。我不确定如何执行此特定任务,是否应该使用Javascript完成?或者我可以使用什么其他方法? 这是我的项目

型号

    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Linq;
    using System.Web;

    namespace webassignment.Models
    {
        public class Donation
        {
            public int ID { get; set; }
            public string DisplayName{ get; set; }
            public DateTime Date { get; set; }
            public decimal Amount { get; set; }
            public decimal TaxBonus { get; set; }
            public string Comment { get; set; }

        }
        public class DonationDBContext: DbContext
        {
            public DbSet<Donation> Donation { get; set; }

        }
  }
索引视图

    <table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.DisplayName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Date)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Amount)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.TaxBonus)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Comment)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.DisplayName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Date)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Amount)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TaxBonus)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Comment)
        </td>
        <td>

        </td>

        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
            @Html.ActionLink("Details", "Details", new { id=item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ID })
        </td>
    </tr>
}




</table>
<script type="text/javascript">

@Html.DisplayNameFor(model=>model.DisplayName)
@DisplayNameFor(model=>model.Date)
@DisplayNameFor(model=>model.Amount)
@DisplayNameFor(model=>model.TaxBonus)
@DisplayNameFor(model=>model.Comment)
@foreach(模型中的var项目){
@DisplayFor(modelItem=>item.DisplayName)
@DisplayFor(modelItem=>item.Date)
@DisplayFor(modelItem=>item.Amount)
@DisplayFor(modelItem=>item.TaxBonus)
@DisplayFor(modelItem=>item.Comment)
@ActionLink(“编辑”,“编辑”,新的{id=item.id})|
@ActionLink(“详细信息”,“详细信息”,新的{id=item.id})|
@ActionLink(“删除”,“删除”,新的{id=item.id})
}

您可以在View中执行此操作,但更好的方法是创建一个ViewModel,我将向您介绍如何暂时在View中执行此操作的基本思路:

@{

   int totalAmount;
   if(Model !=null)
   {
      totalAmount = Model.Sum(x=>x.Amount);
   } 
}
并在需要的任何位置以html格式向下显示:

<h1>@totalAmount</h1>
@totalAmount

您似乎正在向razor视图传递捐赠列表,因此您应该能够在显示器内使用model.Sum(x=>x.Amount)等集合的扩展方法

<h1>@totalAmount</h1>