【问题标题】:The model item passed into the dictionary is 'System.Data.Entity.Infrastructure.DbQuery`, but requires 'System.Collections.Generic.IEnumerable传入字典的模型项是“System.Data.Entity.Infrastructure.DbQuery”,但需要“System.Collections.Generic.IEnumerable”
【发布时间】: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.DbQuery1[&lt;&gt;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


【解决方案1】:

您正在将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; }
}

【讨论】:

  • OP 的视图不期望 TotalMarks
  • @Rahul,嗨,我的模型是使用 DB First Method 自动生成的代码,所以我可以在 Model 类或 SQL Sever Express 中进行更改吗?
  • @stom 你的模型类是一个部分的,所以你可以很容易地扩展它。
  • @stom - 嗨!请不要修改 EF 自动生成的类,您需要创建一个单独的部分类 MarksType 并在其中添加 TotalMarks 属性。
  • @RahulSingh,感谢您的解决方案,我刚刚停止了这种方法,因为我的要求发生了变化。
【解决方案2】:

正如异常所述,您的代码在模型中需要其他类型,而不是您提供的类型:

@model IEnumerable<MvcMarks.Models.MarksType>

检查yourmarks 对象,并相应地更改视图。如我所见,您正在使用总分而不是 MarksType 对象创建查询,因此您的代码无法正常工作。

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

【讨论】:

  • @VMAtm,我正在尝试在视图中显示特定学生的 TotalMarks
  • 考虑@RahulSingh 的答案——我认为他提供了解决您问题的正确方法。
【解决方案3】:

创建一个新的视图模型并使用它来代替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。您应该避免在您的视图中未使用的一些不必要的字段。例如 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 模式。

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

【讨论】:

    猜你喜欢
    • 2022-01-16
    • 2015-09-19
    • 2013-10-17
    • 2014-02-02
    • 1970-01-01
    • 2015-09-19
    • 2020-07-21
    • 1970-01-01
    相关资源
    最近更新 更多