【发布时间】:2014-09-17 17:47:45
【问题描述】:
我正在使用 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);
}
我无法弄清楚我应该在哪里以及如何从通过外键链接到我的表的表(即 Users.UserName 和 Roles.Name)中深度加载数据 - 你能帮忙吗?
【问题讨论】:
标签: c# asp.net-mvc linq entity-framework jqgrid