C# 外键仅显示与主键关联的数字
我想要一种显示外键值而不是显示数字的方式。例如,我有以下视图来显示所有评论:C# 外键仅显示与主键关联的数字,c#,asp.net,asp.net-mvc-3,asp.net-mvc-3-areas,C#,Asp.net,Asp.net Mvc 3,Asp.net Mvc 3 Areas,我想要一种显示外键值而不是显示数字的方式。例如,我有以下视图来显示所有评论: @model IEnumerable<Games.Models.tblReview> @{ ViewBag.Title = "Index"; } <h2>Index</h2> <p> @Html.ActionLink("Create New", "Create") </p> <table> <tr>
@model IEnumerable<Games.Models.tblReview>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
ReviewID
</th>
<th>
Recomendation
</th>
<th>
AvoidOrBuy
</th>
<th>
Score
</th>
<th>
GameIDFK
</th>
<th>
UserName
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.ReviewID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Recomendation)
</td>
<td>
@Html.DisplayFor(modelItem => item.AvoidOrBuy)
</td>
<td>
@Html.DisplayFor(modelItem => item.Score)
</td>
<td>
@Html.DisplayFor(modelItem => item.GameIDFK)
</td>
<td>
@Html.DisplayFor(modelItem => item.UserName)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { id=item.ReviewID }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
</table>
有没有一种方法,我可以获得游戏外键,因为这个网站将用于新来的游戏玩家,我想知道哪些游戏是好的玩不希望用户点击评论,看到的只是一个数字,没有游戏名称
你能帮忙吗
编辑:
在我看来,我使用了以下方法:
@Html.DisplayFor(modelItem => item.tblGame.GameName)
但它会崩溃并产生以下错误:
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
编辑2:
namespace Games.Models
{
using System;
using System.Collections.Generic;
public partial class tblReview
{
public int ReviewID { get; set; }
public string Recomendation { get; set; }
public string AvoidOrBuy { get; set; }
public string Score { get; set; }
public int GameIDFK { get; set; }
public string UserName { get; set; }
public virtual tblGame tblGame { get; set; }
}
<>你可能想考虑一下,但是我想如果你修改你的控制器,使用类似的东西
return View(db.tblReviews.Include("tblGame").ToList());
您可以在视图中使用上述属性。接受率越高,您可能会得到更多帮助。另外,你可以发布你的tblReview modelaliriza adhiyahsi的代码吗?你提供的链接我已经看到了,如果你看我的控制器,我正在使用这个allready,那里有一个返回视图工作得很好:)谢谢,看看东西是否工作,我接受它,因为它工作,人们不理解,尽管人们的代码可能不正确,但我为什么要接受,只是为了保持我的%不变,但感谢起了作用:)
return View(db.tblReviews.Include("tblGame").ToList());