我正在使用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);
}
我无法弄清楚应该在哪里以及如何将通过外键链接到我的表(IE Users.UserName和Roles.Name)的表中的数据深度加载-您能帮忙吗?
_dataContext.UserRoles.Include("Role").Include("User").Where(where).AsQueryable();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句