我有 3 张桌子和 3 个模型。将控件添加到控制文件夹。选择 pages_UsersInRoles 和 UserContext 作为上下文。我需要显示 UserId (Int) UserName (String) 和 UserRoles (String)。无论如何都无法获取用户名?
//my user context and models
enter code here
public class UsersContext : DbContext
{
public UsersContext()
: base("DefaultConnection")
{
}
public DbSet<UserProfile> UserProfiles { get; set; }
public DbSet<webpages_Roles> RolesList { get; set; }
public DbSet<webpages_UsersInRoles> UsersInRole { get; set; }
}
[Table("UserProfile")]
public class UserProfile
{
[Key]
public int UserId { get; set; }
[Required(ErrorMessage = "Department name is required.")]
[MaxLength(50)]
public string UserName { get; set; }
}
[Table("webpages_Roles")]
public class webpages_Roles
{
[Key]
public int RoleId { get; set; }
public string RoleName { get; set; }
public virtual ICollection<webpages_UsersInRoles> Roles { get; set; }
}
[Table("webpages_UsersInRoles")]
public class webpages_UsersInRoles
{
[Key]
[Column(Order = 0)]
public int UserId { get; set; }
// [Key]
[Column(Order = 1)]
public int RoleId { get; set; }
public virtual ICollection<webpages_Roles> Roles { get; set; }
// public virtual ICollection<UserProfile> User { get; set; }
}
///end my controller for index
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace DizelgeMVC.Models
{
public class Default1Controller : Controller
{
private UsersContext db = new UsersContext();
//
// GET: /Default1/
public ActionResult Index()
{
return View(db.UsersInRole.ToList());
}
//
// GET: /Default1/Details/5
public ActionResult Details(int id = 0)
{
webpages_UsersInRoles webpages_usersinroles = db.UsersInRole.Find(id);
if (webpages_usersinroles == null)
{
return HttpNotFound();
}
return View(webpages_usersinroles);
}
//
// GET: /Default1/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Default1/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(webpages_UsersInRoles webpages_usersinroles)
{
if (ModelState.IsValid)
{
db.UsersInRole.Add(webpages_usersinroles);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(webpages_usersinroles);
}
//
// GET: /Default1/Edit/5
public ActionResult Edit(int id = 0)
{
webpages_UsersInRoles webpages_usersinroles = db.UsersInRole.Find(id);
if (webpages_usersinroles == null)
{
return HttpNotFound();
}
return View(webpages_usersinroles);
}
//
// POST: /Default1/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(webpages_UsersInRoles webpages_usersinroles)
{
if (ModelState.IsValid)
{
db.Entry(webpages_usersinroles).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(webpages_usersinroles);
}
//
// GET: /Default1/Delete/5
public ActionResult Delete(int id = 0)
{
webpages_UsersInRoles webpages_usersinroles = db.UsersInRole.Find(id);
if (webpages_usersinroles == null)
{
return HttpNotFound();
}
return View(webpages_usersinroles);
}
//
// POST: /Default1/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
webpages_UsersInRoles webpages_usersinroles = db.UsersInRole.Find(id);
db.UsersInRole.Remove(webpages_usersinroles);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
My View
@model IEnumerable<DizelgeMVC.Models.webpages_UsersInRoles>
@{
var db = new
}
}
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.RoleId)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.RoleId)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.UserId }) |
@Html.ActionLink("Details", "Details", new { id=item.UserId }) |
@Html.ActionLink("Delete", "Delete", new { id=item.UserId })
</td>
</tr>
}
</table>