我有一个声明摘要视图,我需要按月和按年显示客户帐户的总余额。
我创建了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:
我正在努力让它正确显示。
真的很感激任何帮助。谢谢
答案 0 :(得分: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) });
}
}
.....
}