【问题标题】:ASP.Net MVC - Razor Displaying account totals for customers by Month/Year ColumnsASP.Net MVC - Razor 按月/年列显示客户的帐户总数
【发布时间】:2016-09-02 12:38:09
【问题描述】:

我有一个报表摘要视图,我需要按月和年显示客户帐户的总余额。

我已经创建了 linq 查询和视图模型,它们成功地从数据库中提取了这些数据。

我想以表格形式显示这些数据,例如

查看所需



我的 LINQ 代码

var monthlyTotals = from t in db.InvoiceItems.AsEnumerable()
                                 // where t.Invoice.LegalFile.IsClosed == false
                                  group t by t.Invoice.LegalFile.Client into g
                                  select new StatementSummaryVM
                                  {
                                      Client = g.Key,
                                      GrandTotal = g.Sum(x => x.AmountInclVAT),
                                      MonthlyTotals = from i in g
                                              group i by new
                                                    {
                                                        month = i.ItemDate.Month,
                                                        year = i.ItemDate.Year
                                                    }
                                              into d
                                              select new MonthlyTotalsVM
                                              {
                                                  Date = new DateTime(d.Key.year, d.Key.month,1),
                                                  Amount = d.Sum(s => s.AmountInclVAT)
                                              }
                                  };

            return monthlyTotals;

        }

我的视图模型

public class StatementSummaryVM
{
    [Display(Name = "File No.")]
    public int Id { get; set; }

    public Client Client { get; set; }
    public IEnumerable<MonthlyTotalsVM> MonthlyTotals { get; set; }
    [Display(Name = "Grand Total")]
    [DisplayFormat(DataFormatString = "{0:C}")]
    public decimal GrandTotal { get; set; }
}

public class MonthlyTotalsVM
{
    [DisplayFormat(DataFormatString = "{0:MMM-yyyy}")]
    public DateTime Date { get; set; }
    [DisplayFormat(DataFormatString = "{0:C}")]
    public decimal Amount { get; set; }
}

我当前的查看代码

<table class="table">
    <tr>
        <th>File No.</th>
        <th>Client</th>
        <th>Grand Total</th>

        @foreach (var item in Model)
        {
            <th></th>
            foreach (var monthlyTotal in item.MonthlyTotals)
            {
                <th>@Html.DisplayFor(model => monthlyTotal.Date)</th>
            }
        }
    </tr>

    @foreach (var item in Model)
            {
        <tr>
            <td>@item.Client.Id</td>
             <td>@item.Client.Name </td>
             <td>@item.GrandTotal</td>

        @foreach (var monthlyTotal in item.MonthlyTotals)
        {
             <td>@Html.DisplayFor(model => monthlyTotal.Date)</td>
            <td>@monthlyTotal.Amount</td>
        }
        </tr>
    }

</table>

渲染后的样子

当前渲染视图1:

我正在努力让它正确显示。

任何帮助将不胜感激。谢谢

【问题讨论】:

  • 您需要做的第一件事是删除第二个foreach 块中的&lt;td&gt;@Html.DisplayFor(model =&gt; monthlyTotal.Date)&lt;/td&gt;
  • 嗨,斯蒂芬。绝对地。我只是把它留在那里帮助我查看monthlyTotal 模型的日期,以便我可以尝试找出如何构建视图
  • 那你想做什么?您展示了 2 张彼此完全没有关系的图像。如果最后一张图片是“真实”数据,那么您的表格应该是什么样的?
  • 我需要的是在帖子的顶部。在“查看所需”下有一个报告应该是什么样子的 excel sn-p:即在适当的日期列中显示每月总计。
  • 如果您展示真实数据应该是什么样子的实际图像,那么我们可以提供帮助。目前没有任何意义。第一张图的数据和第二张图的数据没有关系!

标签: asp.net-mvc razor linq-to-entities entity-framework-6


【解决方案1】:

您现有的代码仅返回一个实际存在值的 MonthlyTotalsVM 集合,并且使这项工作的关键是在您要显示的日期范围内的每个月使用 MonthlyTotalsVM 初始化集合,并且然后根据月份的索引更新集合中的对应项。

假设您的方法接受参数 startDate 和要在表中显示的月份数,您的代码将是

public ActionResult StatementSummary(DateTime startDate, int months)
{
    // Get the start and end dates
    startDate = new DateTime(startDate.Year, startDate.Month, 1); // ensure first day of month
    DateTime endDate = startDate.AddMonths(months + 1);
    // Initialize the model
    List<StatementSummaryVM> model = new List<StatementSummaryVM>();
    // Filter the data and group by client
    var data = db.InvoiceItems
        .Where(i => i.ItemDate >= startDate && i.ItemDate < endDate)
        .GroupBy(i => i.Invoice.LegalFile.Client);
    // Create a StatementSummaryVM for each client group
    foreach(var clientGroup in data)
    {
        StatementSummaryVM summary = new StatementSummaryVM(startDate, months)
        {
            Client = clientGroup.Key,
            GrandTotal = clientGroup.Sum(x => x.AmountInclVAT)
        };
        // Group by month/year
        foreach(var monthGroup in clientGroup.GroupBy(x => new { Month = x.Date.Month, Year = x.Date.Year }))
        {
            // Get the index of the month
            int index = ((monthGroup.Key.Year - startDate.Year) * 12) + monthGroup.Key.Month - startDate.Month;
            summary.MonthlyTotals[index].Amount = monthGroup.First().AmountInclVAT; // or .Sum(m => m.AmountInclVAT) if there are multiple invoives per month
        }
        model.Add(summary);
    }
    return View(model);
}

然后更改您的 StatementSummaryVM 模型以添加初始化集合的构造函数

public class StatementSummaryVM
{
    public StatementSummaryVM(DateTime startDate, int months)
    {
        MonthlyTotals = new List<MonthlyTotalsVM>();
        for (int i = 0; i < months; i++)
        {
            MonthlyTotals.Add(new MonthlyTotalsVM() { Date = startDate.AddMonths(i) });
        }
    }
    .....
}

【讨论】:

  • 非常感谢斯蒂芬。我做了一些更改,因为我有一个发票项目清单要在发票中总计,但否则您的解决方案就是正确的!我不知道你是怎么做到的,但你是另一回事。谢谢!
猜你喜欢
  • 1970-01-01
  • 2015-11-02
  • 2012-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-11
  • 1970-01-01
相关资源
最近更新 更多