【发布时间】:2021-06-21 10:05:33
【问题描述】:
我正在使用 ASP.NET Core 标识。用户 ID 在 Invite 模型中将是 FK,我正在尝试显示邀请中的所有用户以及所需的信息。
我想在分配给用户的GameID 中显示GameName。
所以它就像在邀请节目中GameName(用户中的FK)GameTag(用户)而不是带有数字的GameID。
模型类:
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 中以获取邀请剃刀索引页面。它只显示 GameID,它是用户内部的 FK,我不知道如何从分配给用户作为 FK 的 Invite 获取游戏 FK 内部的信息
// 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.Id == tu.Id)
{
ViewBag.InviteList.Add(tu.GameID + " " +tu.GameTag);
}
}
}
return View(await _context.Invites.ToListAsync());
}
如果有人理解我的意思,欢迎提出更好的标题
【问题讨论】:
-
只有 3 个模型类。 GameID 将作为 FK 插入到 User Model 中,而 User 将作为 FK 插入到 Invite 中。我想从邀请中获取用户中游戏名称的数据
标签: asp.net-mvc asp.net-core asp.net-mvc-5 asp.net-core-identity