如何从详细信息操作方法中的控制器检索多对多关系中的数据,并将数据放入视图详细信息mvc 4中

时间:2014-04-14 22:48:04

标签: c# asp.net-mvc entity-framework asp.net-mvc-4 ef-code-first

我使用第一个代码创建三个表多对多关系,表UserProfile包含用户,webpages_Roles包含角色和包含{{1}的联接表webpages_UsersInRoles } UsersInRolesUserID

现在我想用他的角色查看每个用户 所以我使用scaffolding选项为连接表创建一个控制器,并将RoleID模型设置为我的模型类,然后创建控制器和视图,当我运行项目时,我转到索引并且它的工作正常它给了我所有的用户和角色,但当我同行详细信息(转到详细信息方法)它没有显示任何东西这是我的模型

UsersInRoles模型

UsersInRoles

角色模型

[Table("UserProfile")]
public class UserProfile
{

    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserID { get; set; }
    public string UserName { get; set; }
    public ICollection<UsersInRoles> UsersInRoles { get; set; }

}

[Table("webpages_Roles")] [Bind(Exclude = "RoleID")] public class Role { [Key] [ScaffoldColumn(false)] public int RoleID { get; set; } [Required(ErrorMessage = "Role Name is required")] [DisplayName("Role Name")] [StringLength(50)] public string RoleName { get; set; } public ICollection<UsersInRoles> UsersInRoles { get; set; } } 模型

UsersInRoles

[Table("webpages_UsersInRoles")] [Bind(Exclude = "UsersInRolesID")] public class UsersInRoles { [Key] public int UsersInRolesID { get; set; } public int UserID { get; set; } public UserProfile UserProfile { get; set; } public int RoleID { get; set; } public Role Role { get; set; } } 控制器

UsersInRoles

public class UsersInRolesController : Controller { private DataBaseContext db = new DataBaseContext(); public ActionResult Index() { var usersinroles = db.UsersInRoles.Include(u => u.UserProfile).Include(u => u.Role); return View(usersinroles.ToList()); } public ActionResult Details(int id) { var usersinroles = db.UsersInRoles.Find(id); if (usersinroles == null) { return HttpNotFound(); } return View(usersinroles); } public ActionResult Create() { ViewBag.UserID = new SelectList(db.UserProfiles, "UserID", "UserName"); ViewBag.RoleID = new SelectList(db.Roles, "RoleID", "RoleName"); return View(); } [HttpPost] public ActionResult Create(UsersInRoles usersinroles) { if (ModelState.IsValid) { db.UsersInRoles.Add(usersinroles); db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.UserID = new SelectList(db.UserProfiles, "UserID", "UserName", usersinroles.UserID); ViewBag.RoleID = new SelectList(db.Roles, "RoleID", "RoleName", usersinroles.RoleID); return View(usersinroles); } public ActionResult Edit(int id = 0) { UsersInRoles usersinroles = db.UsersInRoles.Find(id); if (usersinroles == null) { return HttpNotFound(); } ViewBag.UserID = new SelectList(db.UserProfiles, "UserID", "UserName", usersinroles.UserID); ViewBag.RoleID = new SelectList(db.Roles, "RoleID", "RoleName", usersinroles.RoleID); return View(usersinroles); } [HttpPost] public ActionResult Edit(UsersInRoles usersinroles) { if (ModelState.IsValid) { db.Entry(usersinroles).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.UserID = new SelectList(db.UserProfiles, "UserID", "UserName", usersinroles.UserID); ViewBag.RoleID = new SelectList(db.Roles, "RoleID", "RoleName", usersinroles.RoleID); return View(usersinroles); } public ActionResult Delete(int id = 0) { UsersInRoles usersinroles = db.UsersInRoles.Find(id); if (usersinroles == null) { return HttpNotFound(); } return View(usersinroles); } [HttpPost, ActionName("Delete")] public ActionResult DeleteConfirmed(int id) { UsersInRoles usersinroles = db.UsersInRoles.Find(id); db.UsersInRoles.Remove(usersinroles); db.SaveChanges(); return RedirectToAction("Index"); } protected override void Dispose(bool disposing) { db.Dispose(); base.Dispose(disposing); } } 详细信息视图

UsersInRoles

这就是当我进入索引视图时所发生的事情

当我按详细信息并转到详细信息视图时,这就是hipping

这是我的问题,它只给我用户名和角色名称标签,不显示实际的用户名和角色名称,所以我做错了,谢谢你的帮助

1 个答案:

答案 0 :(得分:1)

我通过更改Details方法来解决问题

public ActionResult Details(int id = 0)
    {

        UsersInRoles usersinroles = db.UsersInRoles.Include(u => u.UserProfile).Include(r => r.Role).Where(i => i.UsersInRolesID == id).SingleOrDefault();
        if (usersinroles == null)
        {
            return HttpNotFound();
        }
        return View(usersinroles);
    }