title: Sum up columns in MVC C# Razor View
body: <p>I am wondering if I can sum up decimal in  all the columns that are dynamically generated in razor view without having to change this in controller and viewModel.  I will be happy to try jQuery option as well.</p>
  
  <p><strong>ViewModel</strong></p>
  
  <pre><code> public class LedgerViewModel
      {
  
      public LedgerViewModel(int PayCategoryCount)
      {
          PayCollection = new List&lt;decimal&gt;(new decimal[PayCategoryCount]);
      }
  public DateTime PDate  { get; set; }
  
  
  public class MonthlyPaymentsVM
  {   public int PayCategoryId { get; set; }
      [DisplayFormat(DataFormatString = "{0:MMMM yyyy}")]
      public DateTime Date { get; set; }
      public IEnumerable&lt;string&gt; PaymentCategories { get; set; }
      public List&lt;LedgerViewModel&gt; Payments { get; set; }
      public List&lt;LedgerViewModel&gt; Member { get; set; }
  } 
  </code></pre>
  
  <p><strong>controller</strong></p>
  
  <pre><code>public ActionResult Report(int year, int no, string nom, Guid gcode)
          {
              DateTime startdate = (new DateTime(DateTime.Now.Year - 1, 7, 1));
              DateTime enddate = (new DateTime(DateTime.Now.Year, 7, 1));
              DateTime date = DateTime.Now;
  
              //get all payments
              var a = from of in db.OfflinePayMents.Include(i =&gt; i.Customer).Include(i =&gt; i.PaymentCategory).ToList().Where(x =&gt; x.PDate &gt;= startdate &amp;&amp; x.PDate &lt; enddate &amp;&amp; x.POK &amp;&amp; x.CustomerGuid=gcode) select new { of.Customer, of.PaymentCategory, of.CustomerID, of.PaymentCategoryID, of.PDate, of.TxnId, of.Pay, of.PType };
  
              var grouped = a.GroupBy(x =&gt; new { customer = x.CustomerID, PaymentCategory = x.PaymentCategory.PaymentCategoryID, txn = x.TxnId, pd = x.PDate, x.PType }).Select(x =&gt; new
              {
                  Name = x.First().Customer.Name,
                  Customer = x.First().Customer,
                  PaymentCategory = x.First().PaymentCategory,
                  Txn = x.First().TxnId,
                  Pd = x.First().PDate,
                  PType = x.First().PType,
                  cid = x.First().PaymentCategoryID,
                  Pay= x.Sum(y =&gt; y.Pay)
  
              });
  
              var data = grouped.GroupBy(x =&gt; x.Txn);
              var PaymentCategories = db.PaymentCategories.OrderBy(z =&gt; z.Order);
              var PayCategoryCount = PaymentCategories.Count();
              var PaymentCategoryIDs = PaymentCategories.Select(x =&gt; x.PaymentCategoryID).ToList();
  
              var model = new MonthlyPaymentsVM()
              {
                  //Member = members,
                  Date = date,
                  PaymentCategories = PaymentCategories.Select(z =&gt; z.PaymentCategoryTitle),
                  Payments = new List&lt;LedgerViewModel&gt;()
              };
  
              foreach (var group in data)
              {
                  LedgerViewModel payment = new LedgerViewModel(PaymentCategoryCount);
                  var pd = group.First().Pd;
                  payment.PDate = pd;
                  foreach (var item in group)
                  {
                      int index = PaymentCategoryIDs.IndexOf(item.PaymentCategory.PaymentCategoryID);
                      if (index &lt; 0)
                      {
  
                          payment.PayCollection[index + 1] = item.Pay;
                      }
                      else
                      {
                          payment.PayCollection[index] = item.Pay;
                      }
                      payment.Total += item.Pay;
                  }
                  model.Payments.Add(payment);
  
              }
  
  
              return View(model);
          }
  </code></pre>
  
  <p><strong>Razor View</strong></p>
  
  <pre><code>&lt;table class="doubleborder" width="99%" border="0" align="center" cellpadding="0" cellspacing="0"&gt;
              &lt;thead&gt;
                  &lt;tr&gt;
                      &lt;th&gt;Date&lt;/th&gt;
                      @foreach (var payname in Model.PaymentCategories)
                      {
                          &lt;th&gt;@payname&lt;/th&gt;
                      }
                  &lt;/tr&gt;
              &lt;/thead&gt;
              &lt;tbody&gt;
                  @foreach (var item in Model.Payments)
                  {
                      &lt;tr&gt;
                          &lt;td&gt;@item.PayDate.ToString("dd/MM/yyyy")&lt;/td&gt;
  
                          @foreach (var amount in item.PayCollection)
                          {
                              &lt;td&gt;@amount.ToString("c")&lt;/td&gt;
                          }
  
  
  
                      &lt;/tr&gt;
                  }
       &lt;tr class="doubleborder"&gt;
       &lt;td&gt;Total:&lt;/td&gt;
        @foreach (var item in Model.PaymentCategories)
        {
        &lt;td&gt;
  looking at getting sum totals of each column here
        &lt;/td&gt;
      }
  
      &lt;/tr&gt;
      &lt;/tbody&gt;
      &lt;/table&gt;
  </code></pre>
  
  <p>So this groups by customer. The payments are grouped by transaction Date and are iterated in the foreach loop in the view.</p>
  
  <p>Thanks if you are able to help.</p>