我有一个返回用户列表的类
public IList<RegisterUsers> GetUsers()
{
var query = (from up in _context.UserProfile
join um in _context.Membership on up.UserID equals um.UserID
select new RegisterUsers { ID = ua.UserID, FirstName = ua.FirstName, LastName = ua.LastName, LastLogIn = up.LastLogIn, Status = up.Status });
return query.ToList();
}
我通过在我的 Action 上执行以下操作来调用这个类
var UserRepository = new UserRepository();
var AllUsers = UserRepository.GetUsers();
var result = from c in AllUsers
select new List<string> { c.ID.ToString(), c.FirstName, c.LastName, c.LastLogIn,c.Status };
return Json(new { sEcho = param.sEcho,
iTotalRecords = result.Count(),
iTotalDisplayRecords = result.Count(),
aaData = result
},
JsonRequestBehavior.AllowGet);
上面的代码工作正常,它返回 JSON 数据。但是我试图弄清楚如果我只是将 AllUsers 直接附加到 aaData 变量,为什么会出现错误。请看下文。
var UserRepository = new UserRepository();
var AllUsers = UserRepository.GetUsers();
return Json(new { sEcho = param.sEcho,
iTotalRecords = AllUsers.Count(),
iTotalDisplayRecords = AllUsers.Count(),
aaData = AllUsers
},
JsonRequestBehavior.AllowGet);