1

I'm working on an AdminPortal and I have a database setup as such:

User 1:N UserRole N:1 Role 1:N RolePage N:1 Page

So, a user is connected to a role via the UserRole table, and the role to a specific page via the RolePage table.

I used MVC5 and the Entity Framework to make my Models and Controllers.

In my UsersController I want the Details to show the Roles as well, which it currently does not. From here someone should be able to pick Edit and then add/remove Roles for the specific User.

When I try to use the existing user.UserRoles, as such:

@Html.DisplayNameFor(model => model.UserRoles)

I get an output of:

System.Data.Entity.DynamicProxies.Role_2928XXXXXXXXXXXXX

And I want to see a list of the Roles assigned to them (i.e., Admin, Sales, etc.) What is the best way to do this??

Here is the Details part of my Controller:

// GET: Users/Details/5
public ActionResult Details(int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    User user = db.Users.Find(id);
    if (user == null)
    {
        return HttpNotFound();
    }
    return View(user);
}
NocFenix
  • 691
  • 5
  • 19
  • How about a partial view? – jamesSampica Aug 28 '15 at 19:54
  • Eagerly load the properties you are going to be using, and use a display/editor template. – Robert McKee Aug 28 '15 at 19:59
  • Alright, I was able to get my Roles to display by doing a foreach loop on the Details.cshtml page. Works pretty now. Now my next step is to make a page that allows the addition/removal of the Roles directly from this page. Any thoughts? – NocFenix Aug 31 '15 at 15:19

2 Answers2

0

Using UserManager.GetRoles will return all the roles of the User

ViewBag.RolesForThisUser = UserManager.GetRoles(user.Id);
MMM
  • 3,132
  • 3
  • 20
  • 32
0

I found the answer on a sort of similar post:

https://stackoverflow.com/a/23536667/3534314

Basically I had to create a view model that held instances of each of my models, and from there I was able to get to all of the information I needed in one view.

Community
  • 1
  • 1
NocFenix
  • 691
  • 5
  • 19