【发布时间】:2015-02-02 00:43:33
【问题描述】:
我是 MVC 新手,正在尝试建立学生评分系统,
模型类如下所示:
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);
}
}
最后我的视图是这样的:
@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__AnonymousType21[System.Nu>llable1[System.Int32]]]',but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[MvcMarks.Models.MarksType]”。
我通过右键单击 MarksProfile 操作方法创建视图并检查强类型视图并选择 MarksType 模型,选择脚手架模板到列表,最后检查创建为部分视图
我可以知道我做错了什么
任何帮助都会很棒。
【问题讨论】:
-
您的查询正在返回一组匿名对象 (
select new {..})
标签: c# asp.net-mvc linq asp.net-mvc-4