C# MVC Razor视图中的汇总列

C# MVC Razor视图中的汇总列,c#,jquery,asp.net-mvc,razor,C#,Jquery,Asp.net Mvc,Razor,我想知道我是否可以将razor视图中动态生成的所有列中的小数相加,而不必在controller和viewModel中进行更改。我也很乐意尝试jQuery选项 视图模型 控制器 剃刀视图 所以这是按客户分组的。付款按交易日期分组,并在视图中的foreach循环中迭代 如果您能提供帮助,谢谢。如果我正确理解了您的模型,这就是您要找的: @Model.Payments.Sum(i => i.Paycollection.Sum(x => x.amount)); 更新,基于您的评论:并假设所

我想知道我是否可以将razor视图中动态生成的所有列中的小数相加,而不必在controller和viewModel中进行更改。我也很乐意尝试jQuery选项

视图模型

控制器

剃刀视图

所以这是按客户分组的。付款按交易日期分组,并在视图中的foreach循环中迭代


如果您能提供帮助,谢谢。

如果我正确理解了您的模型,这就是您要找的:

@Model.Payments.Sum(i => i.Paycollection.Sum(x => x.amount));
更新,基于您的评论:并假设所有列对所有PaymentCollections都有值

@for(int i = 0 ; i < PaymentCategories.Count(); i++)
{
    <td>
        Model.Payments.Sum(x => x.PayCollection[i]); //this will throw an exception if the value at index is not defined
    </td>
}

也许你可以使用linq.Sum方法。i、 e.@payments.Selectx=>x.amount。Sum@Model.Payments.Sumi=>i.Paycollection.Sumx=>x这是正确的,但它将所有列的总和放在一起好的,我现在知道了,看看我在回答中的更新仍然有错误。x.amount未在模型中设置,因此无法获取值。您是否可以共享模型类?它是一个视图模型和其他模型。需要展示的很多内容是付款按日期分组,并按付款类别顺序显示。所以我认为解决方案将包括使用索引和for循环
<table class="doubleborder" width="99%" border="0" align="center" cellpadding="0" cellspacing="0">
            <thead>
                <tr>
                    <th>Date</th>
                    @foreach (var payname in Model.PaymentCategories)
                    {
                        <th>@payname</th>
                    }
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model.Payments)
                {
                    <tr>
                        <td>@item.PayDate.ToString("dd/MM/yyyy")</td>

                        @foreach (var amount in item.PayCollection)
                        {
                            <td>@amount.ToString("c")</td>
                        }



                    </tr>
                }
     <tr class="doubleborder">
     <td>Total:</td>
      @foreach (var item in Model.PaymentCategories)
      {
      <td>
looking at getting sum totals of each column here
      </td>
    }

    </tr>
    </tbody>
    </table>
@Model.Payments.Sum(i => i.Paycollection.Sum(x => x.amount));
@for(int i = 0 ; i < PaymentCategories.Count(); i++)
{
    <td>
        Model.Payments.Sum(x => x.PayCollection[i]); //this will throw an exception if the value at index is not defined
    </td>
}