我有3张桌子和3个型号。添加控件到控制文件夹。选择 webpages_UsersInRoles和UserContext用于上下文。 我需要显示UserId(Int)UserName(String)和UserRoles(String)。 无论如何都不能获得UserName?
//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>
答案 0 :(得分:0)
注意您传递给视图的模型:
@model IEnumerable<DizelgeMVC.Models.webpages_UsersInRoles>
这只是webpages_UsersInRoles
个对象的列表,每个对象只有这些属性:
public int UserId { get; set; }
public int RoleId { get; set; }
public virtual ICollection<webpages_Roles> Roles { get; set; }
但在您看来,您需要的不仅仅是这些信息。具体来说,您还希望在其他模型上显示UserName
属性。要做到这一点,你有几个选择。最直接的两个是:
webpages_UsersInRoles
模型中,该模型可获取所需的值。为简单起见,第二个选项应该是您的第一次尝试。在任何给定webpages_UsersInRoles
对象的上下文中,您有UserId
,因此您可以使用它来获取有关该用户的更多信息。像这样的简单属性可能会起到作用:
public User User
{
get
{
return new UsersContext().UserProfiles.Single(u => u.UserId == this.UserId);
}
}
或者,如果要多次访问它,您可以在本地缓存UserProfile
对象,如下所示:
private User _user;
public User User
{
get
{
if (_user == null)
_user = new UsersContext().UserProfiles.Single(u => u.UserId == this.UserId);
return _user;
}
}
这将在您第一次访问该属性时延迟加载User
,并将其保留在内存中以防您需要再次访问它。然后在您的模型上,您可以通过以下新属性访问UserName
属性:
@foreach (var item in Model)
{
item.User.UserName
}
这里需要注意的一些事项:
var
关键字),这表明出现了问题。模型应该包含视图所需的所有内容,视图应该只访问模型上的内容。如果视图需要新数据,请将其放在模型上。答案 1 :(得分:0)
感谢您的回复!
根据您的建议
将属性添加到现有的webpages_UsersInRoles模型中 获取您需要的值。为简单起见,第二种选择 应该是你的第一次尝试。在任何给定的背景下 webpages_UsersInRoles对象你有一个UserId,所以你可以使用它 获取有关该用户的更多信息。
像这样的简单属性可能会起到作用:
//Added this codes to my webpages_UserInRoles
public User User
{
get
{
return new UsersContext().UserProfiles.Single(u => u.UserId == this.UserId);
}
}
得到这个:
'DizelgeMVC.Models.webpages_UsersInRoles.User' is a 'property'
but is used like a 'type'
当我输入类似public class User
的课程而不是您的建议时,会添加页面顶部using System.Linq;
,但此课程后却不知道UserId