0
当我试图通过LINQ查询的结果为MVC.Webgrid我收到错误(简体):EF MVC的LINQ - 错误传递模型到MVC的WebGrid
The model item passed into the dictionary is of
type'System.Data.Entity.Infrastructure.DbQuery but
this dictionary requires a model item of type
'System.Collections.Generic.IEnumerable`1[MyModel.Models.MyMetric]'
这是的ActionResult失败:
public ActionResult Test()
{
var metrics = db.Metrics
.GroupBy(c => c.Year)
.Select(g => new
{
Year = g.Key,
Q1 = g.Where(c => c.Quarter == 1).Sum(c => c.Value),
Q2 = g.Where(c => c.Quarter == 2).Sum(c => c.Value),
Q3 = g.Where(c => c.Quarter == 3).Sum(c => c.Value),
Q4 = g.Where(c => c.Quarter == 4).Sum(c => c.Value)
});
return View(metrics);
}
我假设,因为我创造,我需要建立某种形式的新模型对原始数据的支点,但我不确定该怎么做,在这种情况下。 (只是让我的脚湿透EF和MVC)
任何想法?
TIA Ĵ
所以我需要创建MyMetric类?正如我目前拥有它,我只是返回“查询”的结果作为匿名类型。 –
是的,你必须创建MyMetrics类。这被称为视图模型。您不能将匿名对象传递给视图。 –
我在你的答案中丢失了一些东西。我试图实现你的示例,仍然会得到相同的错误。 “MyMetric”如何使“指标”成为IEnumerable? –