我是asp.net mvc的新手,我的问题是,我想添加页脚来显示列的总和. 调节器 ListGenerateTicket_Details bk = new ListGenerateTicket_Details();con.Open();string query = "";query += @" select TicketNo,Name,ArrDate,sum(Costume
调节器
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>
希望它能帮到你:)