如何在核心身份中对用户和角色进行排序

问题描述 投票:0回答:2

我正在尝试在我的视图中显示用户的角色列表。

这是我目前的输出:

角色|电子邮件

A | [email protected]

B | [email protected]

B | [email protected]

C | [email protected]

但我希望它是这样的:

角色|电子邮件

A | [email protected]

B | [email protected] < - 按角色排序,然后是电子邮件

B | [email protected]

C | [email protected]

知道如何订购收集的孩子吗?

SettingController.cs

    private readonly UserManager<ApplicationUser> _userManager;
    private readonly RoleManager<ApplicationRole> _roleManager;

    public SettingController(UserManager<ApplicationUser> userManager, RoleManager<ApplicationRole> roleManager)
    {
        _userManager = userManager;
        _roleManager = roleManager;
    }

    public IActionResult Index()
    {
        var usersWithRole = _roleManager.Roles.Include(r => r.UserRoles).ThenInclude(u => u.User).OrderBy(r => r.Name).ToList();

        return View(usersWithRole);
    }

Index.cshtml

@{
    ViewData["Title"] = "Index";
    ViewData["Name"] = "Index";
    int count = 1;
}
<table class="table table-striped bg-white">
    <thead>
        <tr>
            <th>No</th>
            <th>Role</th>
            <th>Email</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var role in Model)
        {
            var result = role.UserRoles;
            @foreach(var userRole in result)
            {
                <tr>
                    <td>@count</td>
                    <td>@role.Name</td>
                    <td>@userRole.User</td>
                </tr>
                count++;
            }   
        }
    </tbody>
</table>

ApplicationRole.cs

public class ApplicationUser : IdentityUser
{
    public ICollection<ApplicationUserRole> UserRoles { get; set; }
}

ApplicationUserRole.cs

public class ApplicationUserRole : IdentityUserRole<string>
{
    public virtual ApplicationUser User { get; set; }
    public virtual ApplicationRole Role { get; set; }
}

ApplicationUser.cs

public class ApplicationUser : IdentityUser
{
    public ICollection<ApplicationUserRole> UserRoles { get; set; }
}

ApplicationDbContext.cs

protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.Entity<ApplicationUserRole>(userRole =>
        {
            userRole.HasKey(ur => new { ur.UserId, ur.RoleId });

            userRole.HasOne(ur => ur.Role)
                .WithMany(r => r.UserRoles)
                .HasForeignKey(ur => ur.RoleId)
                .IsRequired();

            userRole.HasOne(ur => ur.User)
                .WithMany(r => r.UserRoles)
                .HasForeignKey(ur => ur.UserId)
                .IsRequired();
        });
    }
c# asp.net-core-identity asp.net-core-2.2
2个回答
2
投票

使用ThenBy()方法为第二个排序列,如下所示有order by Name, Email asc

 var usersWithRole = _roleManager.Roles.Include(r => r.UserRoles)
                      .ThenInclude(u => u.User)
                      .OrderBy(r => r.Name)
                      .ThenBy(l => l.Email)
                      .ToList();

0
投票

我设法找到一种方法来获得我想要的结果。

我为表创建实体并通过linq访问它,而不是使用Identity UserManager和UserManager。

然后,我将结果绑定到ViewModel并在我的视图中显示它。

调节器

public IActionResult Index()
    {

        //var usersWithRole = _roleManager.Roles.Include(r => r.UserRoles).ThenInclude(u => u.User).OrderBy(r => r.Id).ToList();

        var roleAndUsers = (from r in _context.Role
                     join ur in _context.UserRole
                     on r.Id equals ur.RoleId
                     join u in _context.User
                     on ur.UserId equals u.Id
                     orderby r.Name, u.Email
                     select new UserAndRoleViewModel { Name = r.Name, Email = u.Email, Id = u.Id }).ToList();

        return View(roleAndUsers);
    }

视图模型

public class UserAndRoleViewModel
{
    public string Name { get; set; }
    public string Email { get; set; }
    public string Id { get; set; }
}

视图

@{
    ViewData["Title"] = "Index";
    int count=1;
}
<table class="table table-striped bg-white">
    <thead>
        <tr>
            <th>No</th>
            <th>Role</th>
            <th>Email</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var role in Model)
        {
            <tr>
                <td>@count</td>
                <td>@role.Name</td>
                <td>@role.Email</td>
            </tr>
            count++;
        }
    </tbody>
</table>

ApplicationDbContext

    public DbSet<ApplicationRole> Role { get; set; }
    public DbSet<ApplicationUserRole> UserRole { get; set; }
    public DbSet<ApplicationUser> User { get; set; }
© www.soinside.com 2019 - 2024. All rights reserved.