gpt4 book ai didi

asp.net-mvc - ASP.Net MVC - Razor 按月/年列显示客户的帐户总数

转载 作者:行者123 更新时间:2023-12-04 23:51:48 25 4
gpt4 key购买 nike

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

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

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

查看所需

enter image description here

我的 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;

}

我的 View 模型

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>

渲染时看起来像这样

当前渲染 View 1 : enter image description here

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

非常感谢任何帮助。谢谢

最佳答案

您现有的代码仅返回 MonthlyTotalsVM 的集合,其中实际存在一个值,实现此功能的关键是为每个月使用 MonthlyTotalsVM 初始化该集合在您要显示的日期范围内,然后根据月份的索引更新集合中的相应项目。

假设您的方法接受开始日期参数和要在表中显示的月数,您的代码将是

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) });
}
}
.....
}

关于asp.net-mvc - ASP.Net MVC - Razor 按月/年列显示客户的帐户总数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37089310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com