2

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.IEnumerable1

我的视图期望:@model IEnumerable<OG.Models.UserProfiles>这是一个数据模型而不是 ViewModel。*此视图也是UserProfiles我创建控制器时模型生成的视图(Standart Scaffolding with @Html.DisplayNameFor(model => model.Company)and later @foreach (var item in Model) {to list data`

我的控制器尝试:

  • Attempt 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()
                 }));
    
  • Attempt 2 Aware(不返回列表)

            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 };
    
  • Attempt 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;}
    }

搜索的网站:

4

2 回答 2

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>
于 2013-08-22T14:51:22.407 回答
2

将您的模型转换为列表并将其传递给您的视图,如下所示:

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

 return View(model);
于 2013-08-22T14:51:36.580 回答