传递到字典中的模型项是'System.Data.Entity.Infrastructure.DbQuery`,但需要'System.Collections.Generic.IEnumerable

时间:2015-01-21 07:03:54

标签: c# asp.net-mvc linq asp.net-mvc-4

我是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__AnonymousType2 1 [System.Nu&gt; llable 1[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选中的强类型视图,选择了Standold模板到List,最后选中了create作为局部视图

我可能知道我做错了什么

任何帮助都会很棒。

3 个答案:

答案 0 :(得分:2)

您正在将anonymous type传递给您的视图,但它需要IEnumerable of MarksType,如下所示: -

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的定义,因此您必须将此属性添加到模型中: -

public partial class MarksType
{
    //other properties
    public decimal TotalMarks {get; set; }
}

答案 1 :(得分:1)

如异常所述,您的代码期望模型中的其他类型比您提供的类型:

@model IEnumerable<MvcMarks.Models.MarksType>

检查yourmarks对象,然后相应地更改视图。正如我所看到的,您创建的是带有总分的查询,而不是MarksType个对象,并且您的代码因此而无法正常工作。

你为什么需要TotalMarks财产?你没有在你的视图中使用它。

答案 2 :(得分:0)

创建新的视图模型并使用它而不是MarksType

 public partial class MarksTypeViewModel
 {
    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; }
 }

确保我没有在此模型中添加MarksTypeID。您应该避免使用View中未使用的一些不必要的字段。例如CreateDate,UpdateDate等

现在,您可以在linq语句中使用该视图模型而不是MarksType

List<MarksTypeViewModel> yourmarks = from x in marks.MarksType where x.MarksTypeId == 5
                     group x by 1 into y
                     select new MarksTypeViewModel
                     {   
                         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();

并使用新的ViewModel修改视图页面中的模型绑定。它叫MVVM patern。

@model IEnumerable<MvcMarks.Models.MarksTypeViewModel>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>