2

Visual Studio 2012 Internet Aplication MVC4 C#

ERROR

System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery1, but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1

My View Expects: @model IEnumerable<OG.Models.UserProfiles> this is a dataModel not ViewModel. *This View is also one generate by the UserProfiles Model when I created the Controller ( Standart Scaffolding with @Html.DisplayNameFor(model => model.Company) and later the @foreach (var item in Model) { to list data`

My Controller Attempts:

  • Attempt 1 Aware ( not returning list )

    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 ( not returning list )

            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 ( not returning list )

            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
               };
    
  • Attempt 4 Guess this isn't a "real" list, still errors

    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();
    
  • For attempts 1-3

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

something like that and Created a Viewmodel for them

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

Websites Searched:

Community
  • 1
  • 1
Don Thomas Boyle
  • 3,055
  • 3
  • 32
  • 54

2 Answers2

4

If your view expects IEnumerable<OG.Models.UserProfiles> then this is what you should pass to it, not some anonymous objects that you are projecting in your LINQ queries. If IEnumerable<OG.Models.UserProfiles> is not enough for your view and you need additional information then you'd better create a view model:

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

and then project on this view model:

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);

and finally make your view strongly typed to the same model as what you are passing in your controller action:

@model IEnuemrable<MyViewModel>
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thanks appreciate that, Im still getting used to Types in MVC, this worked for me, just need to work on my Linq query, aparently it doesn't show anyone , reguardless of Admin role or not, but thats neither here nor there. Thanks! +1 – Don Thomas Boyle Aug 22 '13 at 15:04
2

Convert your model to List and pass it to your View like below:

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

 return View(model);
ataravati
  • 8,891
  • 9
  • 57
  • 89
  • 1
    Thanks for the helpful answer, but im also using this for the (Standart view() that requires IEnumerable) :) - sry to not have specified. +1 good answer – Don Thomas Boyle Aug 22 '13 at 15:03