我正在使用ASP.NET Identity 2和Troy Goode PagedList实现ASP.NET MVC 5 Web应用程序。我需要在JqGrid中以以下格式显示UserRole数据:
用户名|角色名称
这是我的UserRole类:
public class UserRole : Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole<int>
{
[ForeignKey("UserId")]
public virtual User User { get; set; }
[ForeignKey("RoleId")]
public virtual Role Role { get; set; }
}
这是我的存储库方法
public virtual IQueryable<UserRole> GetAsQueryable(Expression<Func<UserRole, bool>> where)
{
return _dataContext.UserRoles.Where(where).AsQueryable();
}
这是我的Controller方法(GetAsPagedList方法只是命令输入列表,将PagedList的.ToPagedList方法应用于它并返回它的一个子集):
public ActionResult GridData(MvcJqGrid.GridSettings gridSettings)
{
IQueryable<UserRole> items = _repository.GetAsQueryable();
IPagedList<T> pagedOrderedItems = PagingHelper<UserRole>.GetAsPagedList(
items,
gridSettings.SortOrder, gridSettings.SortColumn,
gridSettings.PageIndex, gridSettings.PageSize);
var jsonData = new
{
total = pagedOrderedItems.TotalItemCount / gridSettings.PageSize + 1,
page = gridSettings.PageIndex,
records = pagedOrderedItems.TotalItemCount,
rows = (
from c in pagedOrderedItems
select new
{
id = c.UserId + '-' + c.RoleId,
cell = new[]
{
"Edit",
"Details",
// TODO: something like this:
// [UserName] = c.User.UserName
// [RoleName] = c.Role.Name
c.Role.Name
}
}).ToArray()
};
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
我无法弄清楚我应该从通过外键链接到我的表(I.E. Users.UserName和Roles.Name)的表深度加载数据的位置和方式 - 你能帮忙吗?
答案 0 :(得分:0)
_dataContext.UserRoles.Include("Role").Include("User").Where(where).AsQueryable();