Navas
Navas

Reputation: 39

ASP.NET MVC: Display user's role

In order to show the name of the user, I use the following:

@HttpContext.Current.User.Identity.Name

Now, I'm having troubles when it comes to display the role name of the user as i don't know how to do it.

Upvotes: 0

Views: 1233

Answers (1)

Rob
Rob

Reputation: 199

Assuming you are using MVC identity for your role stuff. Add the following property to your viewmodel:

public IList<string> RolesforthisUser { get; set; }

In your controller action you can call the roles for the user and populate your viewmodel in the following way:

viewModel.RolesforthisUser = UserManager.GetRoles(userID);

Where userID is your User's ID in the AspNetUsers table.

You can then list out the users roles on the page in the following way:

<ul>
    @foreach (var role in Model.RolesforthisUser)
    {
        <li>
            @role
        </li>
    }
</ul>

Upvotes: 2

Related Questions