【问题标题】:The model item passed into the dictionary is of type 'DbQuery ', but this dictionary requires a model item of type ICollection '传入字典的模型项的类型为“DbQuery”,但此字典需要 ICollection 类型的模型项
【发布时间】:2014-04-17 11:25:07
【问题描述】:

我一直在关注 ASP .NET MVC 教程。我有一个具有以下索引操作的 HomeController。

OdeToFoodDatabase _db = new OdeToFoodDatabase();
public ActionResult Index()
{
    var query = _db.Restaurants.ToList();
    var model = from r in _db.Restaurants
                orderby r.Name ascending
                select r;

    return View(model);
}

我的索引视图是强类型的

@model ICollection<OdeToFood.Models.Restaurant>

现在这给出了错误

传入字典的模型项的类型为'System.Data.Entity.Infrastructure.DbQuery '1[OdeToFood.Models.Restaurant]', but this dictionary requires a model item of type 'System.Collections.Generic.ICollection '1[OdeToFood.Models.Restaurant]'.......

**this error goes away if I pass model.ToList() instead of model in View() function.
But the tutorial that I'm following passed model in View() function and the code was running. I remember myself that some days ago when i tried that it worked fine. Now why is this so?**

有问题的教程是here with chapter "Using LINQ"

【问题讨论】:

  • 返回视图(model.ToList());
  • @BrendanMcKee :请查看编辑

标签: c# sql sql-server asp.net-mvc asp.net-mvc-4


【解决方案1】:

只需在视图中做一个小改动

@model IEnumerable<OdeToFood.Models.Restaurant>

【讨论】:

    【解决方案2】:

    这样做:

    @model List<OdeToFood.Models.Restaurant>
    

    和:

    var model = (from r in _db.Restaurants
                orderby r.Name ascending
                select r).ToList();
    

    或:

    var model = (from r in _db.Restaurants
                orderby r.Name ascending
                select r).ToList<Restaurant>();
    

    【讨论】:

      【解决方案3】:

      您看到的是延迟执行的示例。您的 linq 查询不会在您创建后立即运行,它会在需要时运行。在这种情况下,模型是 DbQuery 类型。当你将它传递给 View 时,它还没有运行。

      调用 model.ToList() 告诉它在此处执行该查询并获取数据。

      正如建议的那样,我相信视图中的代码将是 IEnumerable 而不是 ICollection。如果您查看 DbQuery 类,您会看到

      public class DbQuery<TResult> : IOrderedQueryable<TResult>, 
      IQueryable<TResult>, IEnumerable<TResult>, IOrderedQueryable, IQueryable, 
      IEnumerable, IListSource, IDbAsyncEnumerable<TResult>, IDbAsyncEnumerable
      

      这意味着任何可以使用 DbQuery 的地方都可以使用 IENumerable

      【讨论】:

        【解决方案4】:

        您是否正确地将模型映射到 ViewModel?如果您没有使用 Automapper 或任何类型的映射框架,请使用:

        OdeToFoodDatabase _db = new OdeToFoodDatabase();
        public ActionResult Index()
        
        {
            var query = _db.Restaurants.ToList();
            var model = _db.Restaurants
                         .Take(20)
                         .Select(r => new ResturantViewModel
        
        {  
                              Id=r.Id,
                              Name = r.Name
        
                           });
        
            return View(model);
        }
        

        在您看来,请使用: @model IEnumerable

        【讨论】:

          猜你喜欢
          • 2020-09-08
          • 2013-10-18
          • 1970-01-01
          • 1970-01-01
          • 2013-10-17
          • 2015-09-27
          • 2014-02-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多