Make sure you have
a navigation property[
^] set up on your
UserRole
entity. Then you simply need to
Include
the role, and use the navigation property to display the name.
Entity:
public class UserRole
{
public int UserId { get; set; }
public User User { get; set; }
public int RoleId { get; set; }
public Role Role { get; set; }
}
Controller:
public ActionResult Index()
{
return View(db.UserRoles.Include(ur => ur.User).Include(ur => ur.Role).ToList());
}
View:
@foreach (UserRole item in Model)
{
<p>User @item.User.Name is in role @item.Role.Name</p>
}