0

I'm trying to show a user profile on my site.

For example: "Rank: Owner" But I don't know how to display the rank name. This is the code I currently have:

<p>@if (Model.Roles.Any() != false)
             {

             <b>@Model.Roles.Any()</b>

             }
             else
             {
                 <b>Member</b>
             }
    </p>

But that does only give me this:

System.Collections.Generic.List`1[Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole]

So what do I need to get my role name out?

UnScope
  • 169
  • 1
  • 2
  • 10
  • `if(Model.Roles.Any()!= null)` is always true. instead `if(Model.Roles.Any())` – tchelidze Jan 09 '16 at 12:55
  • what is a structure of `IdentityUserRole` ? – tchelidze Jan 09 '16 at 12:56
  • @tchelidze I'm doing that because that if it is null then they get member. And it is the ussal structure – UnScope Jan 09 '16 at 12:58
  • 1
    note that `Model.Roles.Any()` can not be null, since `bool` is not nullable type. If `Model` or `Model.Roles` can be null, use [null propagation ](http://davefancher.com/2014/08/14/c-6-0-null-propagation-operator/) instead `Model?.Roles?.Any()` – tchelidze Jan 09 '16 at 13:02
  • I believe its list of roles. Try creating a method to return string representation of array of roles – Anup Sharma Jan 09 '16 at 13:26

2 Answers2

0

First of all @Model.Roles is a list of type IdentityUserRole. So if you wish to see something from that collection you have to iterate it.

// this will never generate readable output, it will always print something like this: System.Collections.Generic.List`1[Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole]
<b>@Model.Roles</b>

Second: the type IdentityUserRole doesn't contain any property that contains the rolename. Only the roleid and the userid. I think you have to get those from somewhere else

Get Role name in IdentityUserRole 2.0 in ASP.NET

Community
  • 1
  • 1
Tom B.
  • 2,892
  • 3
  • 13
  • 34
0

Try this code

public static class Extension
{
   public static string GetRoleList(this List<Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole> roles)
   {
      string rolelist="";
      if(roles.Count==0)
         return "None";
      foreach (var item in roles)
      {
         rolelist+=item+",";
      }
      return rolelist.TrimEnd(',');
   }
}

and then use this in view as

<b>@Model.Roles.GetRoleList()</b>
Anup Sharma
  • 2,053
  • 20
  • 30