传递到字典中的模型项是"System.Data.Entity.Infrastructure.DbQuery",但需要"System.Collections.Generic.IEnumerable" [英] The model item passed into the dictionary is 'System.Data.Entity.Infrastructure.DbQuery`, but requires 'System.Collections.Generic.IEnumerable
问题描述
我是MVC的新手,正在尝试建立学生评分系统
i am new to MVC, and trying to build students marks system,
模型类如下:
public partial class MarksType
{
public int MarksTypeId { get; set; }
public Nullable<int> English { get; set; }
public Nullable<int> Math { get; set; }
public Nullable<int> Physics { get; set; }
public Nullable<int> Chemistry { get; set; }
public Nullable<int> ComputerScience { get; set; }
public Nullable<int> MoralScience { get; set; }
public Nullable<int> TotalMarks { get; set; }
}
我的控制器如下:
public class MarksController : Controller
{
MarksEntities marks = new MarksEntities();
public ActionResult MarksProfile()
{
List<MarksType> yourmarks = from x in marks.MarksType where x.MarksTypeId == 5
group x by 1 into y
select new MarksType
{
English = y.Sum(x => x.English),
Math = y.Sum(x => x.Math),
Physics = y.Sum(x => x.Physics),
Chemistry= y.Sum(x => x.Chemistry),
ComputerScience = y.Sum(x => x.ComputerScience),
MoralScience= y.Sum(x => x.MoralScience),
TotalMarks = y.Sum(x => x.English) + y.Sum(x => x.Math)
+ y.Sum(x => x.Physics) + y.Sum(x => x. Chemistry)
+y.Sum(x => x.ComputerScience) + y.Sum(x => x.MoralScience)
}).ToList();
return View(yourmarks);
}
}
最后,我的视图看起来像这样:
Finally my View Looks Like this:
@model IEnumerable<MvcMarks.Models.MarksType>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model. MarksTypeId)
</th>
<th>
@Html.DisplayNameFor(model => model.English)
</th>
<th>
@Html.DisplayNameFor(model => model.Math)
</th>
<th>
@Html.DisplayNameFor(model => model.Physics )
</th>
<th>
@Html.DisplayNameFor(model => model.Chemistry)
</th>
<th>
@Html.DisplayNameFor(model => model.ComputerScience)
</th>
<th>
@Html.DisplayNameFor(model => model.MoralScience)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.MarksTypeId)
</td>
<td>
@Html.DisplayFor(modelItem => item.English)
</td>
<td>
@Html.DisplayFor(modelItem => item.Math)
</td>
<td>
@Html.DisplayFor(modelItem => item.Physics )
</td>
<td>
@Html.DisplayFor(modelItem => item.Chemistry)
</td>
<td>
@Html.DisplayFor(modelItem => item.ComputerScience)
</td>
<td>
@Html.DisplayFor(modelItem => item.MoralScience)
</td>
</tr>
}
</table>
我收到此错误:
传递到字典中的模型项的类型为'System.Data.Entity.Infrastructure.DbQuery
1[<>f__AnonymousType2
1 [System.Nu> llable1[System.Int32]]]',but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1 [MvcMarks.Models.MarksType]'".
The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery
1[<>f__AnonymousType2
1[System.Nu>llable1[System.Int32]]]',but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1[MvcMarks.Models.MarksType]'.
我通过右键单击MarksProfile Action Method创建了View,并检查了选中MarksType Model的强类型视图,将脚手架模板选择为List,最后选中了创建为局部视图
i created View by right clicking on MarksProfile Action Method and checked strongly typed view with MarksType Model selected , selected scaffold template to List ,and finally checked create as a partial view
我可以知道我在做什么错
may i know what i am doing wrong
任何帮助都会很棒.
推荐答案
您正在将anonymous type
传递给视图,但是它期望使用IEnumerable of MarksType
,如下所示:-
You are passing an anonymous type
to your view, but it expects an IEnumerable of MarksType
, something like this:-
List<MarksType> yourmarks = (from x in marks.MarksType where x.MarksTypeId == 5
group x by 1 into y
select new MarksType
{
MarksTypeId = .. ,
English = ..
TotalMarks = y.Sum(x => x.English) +y.Sum(x => x.Math)
+ y.Sum(x => x.Physics) + y.Sum(x => x. Chemistry)
+y.Sum(x => x.ComputerScience) + y.Sum(x => x.MoralScience)
}).ToList();
但是,由于您的模型MarksTypeId
不包含MarksType
的定义,因此您必须将此属性添加到模型中:-
But, since your Model MarksTypeId
does not contain a definition of MarksType
, you will have to add this property to your model:-
public partial class MarksType
{
//other properties
public decimal TotalMarks {get; set; }
}
这篇关于传递到字典中的模型项是"System.Data.Entity.Infrastructure.DbQuery",但需要"System.Collections.Generic.IEnumerable"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!