我使用以下代码从数据库中选择一些文件:
var _Users = (from s in db.Users
from ur in s.Roles
join r in db.Roles on ur.RoleId equals r.Id
select new
{
s.Id,
s.Email,
s.FirstName,
s.LastName,
Name = s.UserName,
Role = r.Name,
});
并将结果发送到视图:
return View("Index", _Users.ToPagedList(pageNumber, pageSize));
在视图中:
@using PagedList.Mvc
@model PagedList.IPagedList<FinalKaminet.Models.ApplicationUser>
但我收到了这个错误:
传递到字典中的模型项是类型的 'PagedList.PagedList
1[<>f__AnonymousType10
6 [System.String,System.String,System.String,System.String,System.String,System.String]]', 但是这个字典需要一个类型的模型项 'PagedList.IPagedList`1 [FinalKaminet.Models.ApplicationUser]'。
出了什么问题?
答案 0 :(得分:0)
在选择中,您没有定义要传递给视图的类型,请将其更改为select new ApplicationUser
var _Users = (from s in db.Users
from ur in s.Roles
join r in db.Roles on ur.RoleId equals r.Id
select new MyViewModel
{
firstproperty = s.Id,//initialize every property like this
s.Email,
s.FirstName,
s.LastName,
Name = s.UserName,
Role = r.Name,
}).ToList();//call to List
class MyViewModel
{
//define properties you needto show in view
}