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
我创建控制器时模型生成的视图(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;}
}
搜索的网站: