Linq to View错误,传递通用列表:必需的IEnumerable

时间:2013-08-22 14:45:56

标签: asp.net-mvc linq-to-entities viewmodel

Visual Studio 2012 Internet应用程序MVC4 C#

错误

  

System.InvalidOperationException:传入字典的模型项类型为'System.Data.Entity.Infrastructure.DbQuery 1, but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable 1

我的观点预期:@model IEnumerable<OG.Models.UserProfiles>这是数据模型而不是ViewModel。 *当我创建控制器时,此视图也是UserProfiles模型生成的视图(标准脚手架@Html.DisplayNameFor(model => model.Company),后来@foreach (var item in Model) {列出数据

我的控制器尝试:

  • 尝试1 Aware(不返回列表)

    return View(db.UserProfiles
                 .Include(c => c.Roles)
                 .Where(c => c.Roles.Any(up => up.RoleName != "Administrator"))
                 .Select(c => new
                 {
                     UserID = c.UserID,
                     UserName = c.UserName,
                     UserCount = c.Roles.Count()
                 }));
    
  • 尝试2 意识到(不返回列表)

            var model =
                from usr in db.UserProfiles
                join rls in db.UserRoles on usr.Roles equals rls.RoleId
    
                select new { UserID = usr.UserID, UserName = usr.UserName };
    
  • 尝试3 Aware(不返回列表)

            var model =
               from usr in db.UserProfiles.Include(t => t.Roles)
               where usr.Roles.Any(up => up.RoleName != "Administrator")
               select new WebAdminUsersLookup
               {
                   UserID = usr.UserID,
                   UserName = usr.UserName
               };
    
  • 尝试4 猜猜这不是“真实”列表,仍然是错误

    var listOfIdeas = 
         (from x in db.UserProfiles.Include(t => t.Roles)
         .Where(u => u.Roles.Any(up => up.RoleName != "Administrator"))
         select new { UserID = x.UserID, UserName = x.UserName }).ToList();
    
  • 对于尝试1-3

            List<UserProfiles> modelList = new List<UserProfiles>();
            modelList.Add(model.ToList());
    

类似的东西和为他们创建了一个Viewmodel

    private class WebAdminUsersLookup
    {
        //public List<UserProfiles> Users { get; set; }
        public int UserID { get; set; }
        public string UserName { get; set;}
    }

网站搜索:

2 个答案:

答案 0 :(得分:4)

如果您的视图需要IEnumerable<OG.Models.UserProfiles>,那么您应该将其传递给它,而不是您在LINQ查询中预测的一些匿名对象。如果IEnumerable<OG.Models.UserProfiles>不足以满足您的观看需要其他信息,那么您最好创建一个视图模型:

public class MyViewModel
{
    public int UserID { get; set; }
    public string UserName { get; set; }
    public int RolesCount { get; set; }
}

然后在此视图模型上投影:

IEnumerable<MyViewModel> model = db
    .UserProfiles
    .Include(c => c.Roles)
    .Where(c => c.Roles.Any(up => up.RoleName != "Administrator"))
    .Select(c => new MyViewModel
    {
        UserID = c.UserID,
        UserName = c.UserName,
        RolesCount = c.Roles.Count()
     });

return View(model);

最后将您的视图强类型设置为与控制器操作中传递的模型相同的模型:

@model IEnuemrable<MyViewModel>

答案 1 :(得分:2)

将您的模型转换为List并将其传递给您的View,如下所示:

 var model = db.UserProfiles
 .Include(c => c.Roles)
 .Where(c => c.Roles.Any(up => up.RoleName != "Administrator")).ToList();

 return View(model);