public ActionResult Index()
{
var groups = db.SHP_Products
.GroupBy(c => c.SHP_Category.Name,
(category, items) => new
{
CategoryName = category,
ItemCount = items.Count(),
Items = items
}
);
ViewBag.group = groups.ToList();
return View();
}
运行时会显示如下错误:
<ul>
@foreach (var m in ViewBag.group)
{
<h2>@m.CategoryName</h2>
<a href="#" class="prev1">Previous</a><a href="#" class="next1">Next</a>
<li></li>
}
</ul>
'object' does not contain a definition for 'CategoryName'
您正在将匿名对象列表传递给视图。
看看这个答案 Dynamic Anonymous type in Razor causes RuntimeBinderException