Martin McMahon
Martin McMahon

Reputation: 333

Displaying user role in a partial view

I have a partial view within my main _Layout.cshtml. The partial view is used to display info about the logged in user.

The username can be displayed using @Context.User.Identity.Name

but I also want to display the role of the user.

What would be the best way to display this info?

Upvotes: 2

Views: 815

Answers (1)

user596075
user596075

Reputation:

Loop through all of the user's roles in the View and display them accordingly:

@foreach(string role in Roles.GetRolesForUser(Context.User.Identity.Name))
{
    @Html.Label(role);
}

This utilizes the Roles.GetRolesForUser() method, and returns a string[] for all the roles (each represented as a string) the user is a part of.

Upvotes: 3

Related Questions