所以我一直在搜索Google和SO.这个问题的感觉已被多次询问,但没有答案对我有帮助,但我觉得我已经接近了.但是,我是LINQ和Lambda的新手,并且没有做我想做的事情的知识.
User Roles ----------------------------------------- John Admin Jane Staff, HR, Payroll MyCoolUserName User
我非常接近这篇文章和这篇文章.这是我到目前为止所得到的.
public class UsersViewModel { [Display(Name = "User")] public ApplicationUser User { get; set; } [Display(Name = "Roles")] public string Roles { get; set; } }
此解决方案返回角色的空白,我不得不将其添加到我的web.config
文件中:
public class UsersController : Controller { public async Task Index() { var allUsers = await db.Users.ToListAsync(); var users = new List(); foreach (var user in allUsers) { String[] roles = Roles.GetRolesForUser(user.UserName); users.Add(new UsersViewModel {User = u, Roles = String.Join(",", roles.ToArray())}); } return View(users); } }
此解决方案为每个角色的每个用户返回一行,但仅返回 RoleId
public class UsersController : Controller { public async Task Index() { var allUsers = await db.Users.ToListAsync(); var users = allUsers.Select(u => new UsersViewModel {User = u, Roles = String.Join(",", u.Roles.Select(r => r.RoleId))}).ToList(); return View(users); } }
以下是我改为试用#2时RoleId
获得的结果RoleName
:
我可以说,在试验#2中,u.Roles
它与UserRoles
表格相关联.从逻辑上讲,我知道我想要的是内部加入Roles
表并在那里得到名称.
我希望有人可以帮助我吗?提前致谢.示例项目