我對MVC5非常陌生,正在嘗試顯示用戶詳細信息的列表視圖以及他們所在角色的名稱。我使用MVC標識設置了角色。列表視圖顯示角色和應用程序用戶
下面是我的ViewModel
public class UserRoleViewModel
{
public string UserRoleVMId { get; set; }
public string Title { get; set; }
public string FirstName { get; set; }
public string Surname { get; set; }
public string Email { get; set; }
public string Role { get; set; }
public bool AccountEnabled { get; set; }
}
而且我的控制器:
public ActionResult Index()
{
var model = (from d in db.Users
select new UserRoleViewModel()
{
UserRoleVMId = d.Id,
Title = d.Title,
FirstName = d.FirstName,
Surname = d.Surname,
Email = d.Email,
AccountEnabled = d.AccountEnabled,
Role = d.Role.Name
}).ToList();
return View(model);
}
我不確定如何顯示角色名稱和正在錯誤消息
does not contain the definition for Role and no extension method Role
在
Role = d.Role.Name
任何幫助,將不勝感激
感謝
你必須加入角色表用戶表來獲得角色的名字。 – Azim