【发布时间】:2017-09-15 21:17:23
【问题描述】:
我有这个方法应该显示最受欢迎/最喜欢的产品,并根据每个产品的数量总和降序排列。虽然它仍然需要测试。我需要将此方法ProductPop() 从控制器调用到视图中。那是没有链接到控制器的
public class AnalysisController : Controller
{
public List<Order_Detail> ProductPop()
{
List<Order_Detail> result = new List<Order_Detail>();
result.GroupBy(l => l.ProductName).Select(cl => new Order_Detail
{
ProductName = cl.First().ProductName,
Quantity = cl.Sum(c => c.Quantity)
}).OrderByDescending(k=>k.Quantity).ToList();
return result;
}
}
这就是我的视图中的内容(这个视图是另一个控制器的空索引方法,它只返回一个视图):
@using ProjectName.Controllers;
@{
AnalysisController AC = new AnalysisController();
ViewBag.Title = "Index";
}
@foreach(var od in ProductPop())
{
<table>
<tr>
<th>
Product Name
</th>
<th>
Quantity
</th>
</tr>
<tr>
<td>
@od.ProductName
</td>
<td>
@od.Quantity
</td>
</tr>
</table>
}
如果有任何参考/建议。我会很感激的。谢谢!
【问题讨论】:
-
你试过阅读基础 ASP.NET MVC 教程吗?
标签: c# asp.net-mvc list razor lambda