70231

How to iterate Roles in IEnumerable and display role names in razor view

Am using Identity 2.1.0 with MVC and razor Views. One of my views gets a list of all users with this GET Controller Action:

public async Task<ActionResult> Users() { return View(await UserManager.Users.ToListAsync()); }

Within the View there is a foreach that iterates through each user and displays the usual information like email address, username, etc. I would like to know if there is also a way of iterating through the roles for each user so that I can display the role membership of each user something like this:

Customer Employee

I haven't been able to figure this out. Part of my problem is that I don't have any Intellisense within razor views and haven't been able to find anything helpful on Google.

The View Model is @model IEnumerable<XXXX_My_App.Models.ApplicationUser>. ApplicationUser has a base class of IdentityUser.

Answer1:

The easiest way would be to create a custom ViewModel which contains the user itself and the roles he is member of. An implementation could look like the following:

<strong>Controller</strong>

public async Task<ViewResult> Users() { var users = UserManager.Users; var model = new Collection<UserRoleViewModel>(); foreach (var user in users) { var roles = await UserManager.GetRolesAsync(user.Id); var rolesCollection = new Collection<IdentityRole>(); foreach (var role in roles) { var role = await RoleManager.FindByNameAsync(roleName); rolesCollection.Add(role); } model.Add(new UserRoleViewModel { User = user, Roles = rolesCollection }); } return View("Users", model); }

<strong>UserRoleViewModel</strong>

public class UserRoleViewModel { public ApplicationUser User { get; set; } public Collection<IdentityRole> Roles { get; set; } }

With that you can use the following model in your view to iterate over the appropriate properties

@model ICollection<YourProject.Models.UserRoleViewModel>

Answer2:

If your Usermanager is returning a list of identityusers and so each identityuser should have a roles property which you should be able to enumerate. However this will only be populated if your implementation includes a class which implements IRoleStore but you haven't provided details of your implementation so we can only guess.

http://msdn.microsoft.com/en-us/library/dn613256(v=vs.108).aspx

Recommend

  • How to count in nested if statement in matlab?
  • How to get point of a figure (using callbacks in GUI) when the point is inside a region that previou
  • Different CUDA versions shown by nvcc and NVIDIA-smi
  • Why does this MATLAB 'if' statement not work?
  • Remove legend ggplot 2.2
  • How do you pass in a value to a subfunction in Matlab I am getting output errors?
  • How can I change the constraint of line profile?
  • in R, how to calculate mean of all column, by group?
  • Equation of the parabola enclosing a scatter plot
  • Extract single plot from ggplot with facet_grid
  • Fibonacci numbers using matlab [duplicate]
  • How to click on a coordinate on a screen?
  • Python. How to optimize search functions
  • simplemembership MVC4 get username by userId
  • Iterating through a list to create new columns in a dataframe
  • Pandas data types change when iterating over the major axis
  • Convert “String” of Binary to NSString of text
  • How would you audit ASP.NET Membership tables, while recording what user made the changes?
  • Making a switch statement in C with an array?
  • How to redirect or show a page rather than “Forbidden” when i have directory listings off (htaccess/
  • Symfony2 plaintext users don't work
  • Yii2 Login with database
  • Can't access Tomcat 7 Manager app when running from Eclipse
  • Iterate twice through a DataReader
  • Is it better to have roles as a column on my users table, or do it through join tables (Roles &
  • Linking Ghostscript to pypdfocr in Windows Platform
  • my tic-tac-toe program in matlab does not work [closed]
  • Action Pack components in Rails
  • Javascript/Jquery runs fast in desktop browsers, but slow in mobile/smartphone browsers…should I spl
  • Button click event not firing in jQuery
  • Submit form in a displaytag pagination
  • ActionScript 2 vs ActionScript 3 performance
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • To display the title for the current loaction in map in iphone
  • Web-crawler for facebook in python
  • Traverse Array and Display in markup
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • How to set the response of a form post action to a iframe source?
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection