【发布时间】:2018-10-26 18:45:27
【问题描述】:
我想在 Razor 视图中显示查询结果。
我通过控制器获得了所需的输出,但我需要帮助才能在视图中显示。
班级Fuel:
public class Fuel
{
public int FuelId { get; set; }
public int FMonth{ get; set; }
public int Year { get; set; }
}
班级Location:
public class Location
{
public int LocationId { get; set; }
public string Name { get; set; }
public string Address { get; set; }
}
现在我使用视图模型类 FuelLocation 加入两个表:
public class FuelLocation
{
public Fuel Fuels { get; set; }
public Location Locations { get; set; }
}
这是控制器中的操作方法:
public ActionResult EmpDetails()
{
var res = (from loc in _context.Locations
join Fl in _context.Fuels on loc.Name equals Fl.Locationn
select new FuelLocation
{
Fuels = Fl,
Locations = loc
}).GroupBy(c => c.Fuels.Locationn)
.Select(g => new
{
Location = g.Key,
Jan = g.Where(c => c.Fuels.FMonth == 1).Sum(c => c.Fuels.Sale),
Feb = g.Where(c => c.Fuels.FMonth == 2).Sum(c => c.Fuels.Sale),
March = g.Where(c => c.Fuels.FMonth == 3).Sum(c => c.Fuels.Sale)
}).ToList();
}
这个查询的结果是:
我想这样展示它
Name | Jan | Feb | Mar | Total
1 | 100 | 350 | 250 | 700
2 | 200 | 220 | 2150 | 2170
我的剃刀视图:
@model IList<COSAuthNew.Services.Generic.FuelLocation>
<h2>Index</h2>
<table class="table">
<tr>
<th>LocationName</th>
<th>Jan</th>
<th>Feb</th>
<th>March</th>
<th>Total</th>
</tr>
@foreach (var item in Model)
{
//Plz enter your solution
}
但我没有得到我想要的正确结果。
谢谢
【问题讨论】:
标签: c# entity-framework asp.net-core