【发布时间】:2021-06-20 19:23:20
【问题描述】:
我正在使用 asp.net 核心身份。用户 ID 在 Invite 模型中将是 FK,我正在尝试显示邀请中的所有用户
型号
public class Invite
{
public int ID { get; set; }
[ForeignKey("UserId")] //ICollection<Invite> in User
[Display(Name = "Users")]
public virtual ApplicationUser User { get; set; }
}
=================
public class ApplicationUser : IdentityUser
{
public string Description { get; set; }
[ForeignKey("GameID")]
public int? GameID { get; set; }
public string GameTag { get; set; }
public virtual ICollection<Invite> Invite { get; set; }
================
public class Game
{
public int ID { get; set; }
[Display(Name = "Game")]
public string GameName { get; set; }
public virtual ICollection<ApplicationUser> ApplicationUser { get; set; }//Allow Users to get Games FKID (Foreign key ID)
}
在邀请控制器索引中获取邀请列表并将它们放入viewbag中以获取邀请剃刀索引页面
这是错误的来源
if (i.User == tu.Id)
// GET: Invites
public async Task<IActionResult> Index()
{
ViewBag.InviteList = new List<String>();
var invite = _context.Invites;
var theuser = _context.ApplicationUser;
foreach (Invite i in invite)
{
foreach (ApplicationUser tu in theuser)
{
if (i.User == tu.Id)
{
ViewBag.InviteList.Add(tu.GameID + " " +tu.GameTag);
}
}
}
return View(await _context.Invites.ToListAsync());
}
【问题讨论】:
-
我假设您的
Invite模型中还缺少实际的外键属性UserId。有了这个,它将是if (i.UserId == tu.Id)
标签: asp.net-mvc asp.net-core asp.net-mvc-5 asp.net-core-identity