我有這種方法,應顯示最受歡迎/最喜愛的產品和順序降序根據每個產品的數量總和。儘管它仍然需要測試。我需要從控制器調用這個方法ProductPop()
進入View。未鏈接到控制器如何顯示在查看傳遞從controller.MVC項目列表
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教程? – Marusyk