I am trying to display a row of each user and their roles in the ASP.net MVC application.
Users and their roles are stored in ASP.net membership provider table. Using the code below I can display each user and the role they are assigned, but my problem is if the user is in multiple roles then the user gets repeated.
I tried making UserRole as List but I am now stuck as I do not know how to add the roles to a list in the code below. Could you please provide me some direction?
My viewmodel is :
public class UserViewModel
{
public string UserName { get; set; }
public List<String> UserRole { get; set; }
}
var roles = ApplicationRoles.RolesList;
var users = new List<UserViewModel>();
foreach (var role in roles)
{
foreach (var user in Roles.GetUsersInRole(role))
{
users.Add(new UserViewModel {UserName = user, UserRole = role });
}
}
return View(users);
How can I load the roles so I do not repeat username if the user has multiple roles attached to him/her?
I would like to display it in the following format:
=======================
User | Role
John | Admin, Approver
Sam | Approver
=======================