gpt4 book ai didi

c# - 如何在 MVC 中显示列总和

转载 作者:行者123 更新时间:2023-11-30 14:22:22 24 4
gpt4 key购买 nike

我是 ASP.NET MVC 新手,我的问题是,我想添加页脚以显示列的总和。

Controller

List<GenerateTicket_Details> bk = new List<GenerateTicket_Details>();
con.Open();
string query = "";

query += @" select TicketNo,Name,ArrDate,sum(CostumeQuantity) As Quantity, sum(CostumeTotalPrice) As Total, sum(ISNULL(RefundAmount,0)) AS REFUND
,sum(ISNULL(CostumeTotalPrice,0) - ISNULL(RefundAmount,0)) AS TotalAmount from GenerateTicket where 1=1";
query += @" Group by TicketNo,Name,ArrDate ";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
cmd.CommandType = CommandType.Text;
DataSet ds = new DataSet();
da.Fill(ds);
foreach (DataRow dr in ds.Tables[0].Rows)
{
bk.Add(new GenerateTicket_Details() { TicketNo = dr[0].ToString(), name = dr[1].ToString(), ArrDate = dr[2].ToString(), quantity = dr[3].ToString(), Total = Convert.ToDecimal(dr[4].ToString()), Refund = Convert.ToDecimal(dr[5].ToString()), TotalAmount = Convert.ToDecimal(dr[6].ToString()) });
string orderDetails = dr[3].ToString();

}
cmd.ExecuteNonQuery();
ViewBag.MyCustomCollection = bk;
return View(bk);

查看

<tbody>
@foreach (var item in Model)
{
<tr>

<td style="padding-left: 12px;">@Html.DisplayFor(modelItem => item.TicketNo)</td>
<td style="padding-left: 12px; display: none;">@Html.DisplayFor(modelItem => item.name)</td>
<td style="padding-left: 12px;">@Html.DisplayFor(modelItem => item.ArrDate)</td>
<td style="padding-left: 12px;">@Html.DisplayFor(modelItem => item.quantity)</td>
<td style="padding-left: 12px;">@Html.DisplayFor(modelItem => item.Total)</td>
<td>@Html.DisplayFor(modelItem => item.Refund)</td>
<td>@Html.DisplayFor(modelItem => item.TotalAmount)</td>
</tr>
}
</tbody>

我想在列表的最后显示 TotalAmount 的总和

最佳答案

C# 代码:

ViewBag.TotalAmount = ds.Tables[0].Select().Sum(w=> (int) w["RefundAmount"] );
  • ds 是您的数据集对象
  • (int) w["RefundAmount"] 使用您的类型和属性

html:

<tbody>
@foreach (var item in Model)
{
.......
}
<tr><td>TotalAmount : @ViewBag.TotalAmount</td></tr>
</tbody>

希望对你有帮助:)

关于c# - 如何在 MVC 中显示列总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50073276/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com