2012-04-07 6 views
0

番号を表示するのではなく、外部キーの値を表示する方法が必要です。例えば、私はすべてのレビューを表示するには、次のビューがあります:外部キープライマリキーに関連付けられている番号を表示するだけです

@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> 

私はGameIDFKは、この情報を表示するために私のコントローラは、基本である数はここにあなたが表示されますではなく、ゲームの名前が表示されている方法をしたいです

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using Games.Models; 

namespace Games.Controllers 
{ 
    public class ShowAllReviewsController : Controller 
    { 
     // 
     // GET: /ShowAllReviews/ 

     public ActionResult Index() 
     { 
      using (var db = new gamezoneDBEntities()) 
      { 

       return View(db.tblReviews.ToList()); 
      } 
     } 

     // 
     // GET: /ShowAllReviews/Details/5 

     public ActionResult Details(int id) 
     { 
      return View(); 
     } 

     // 
     // GET: /ShowAllReviews/Create 

     public ActionResult Create() 
     { 
      return View(); 
     } 

     // 
     // POST: /ShowAllReviews/Create 

     [HttpPost] 
     public ActionResult Create(FormCollection collection) 
     { 
      try 
      { 
       // TODO: Add insert logic here 

       return RedirectToAction("Index"); 
      } 
      catch 
      { 
       return View(); 
      } 
     } 

     // 
     // GET: /ShowAllReviews/Edit/5 

     public ActionResult Edit(int id) 
     { 
      return View(); 
     } 

     // 
     // POST: /ShowAllReviews/Edit/5 

     [HttpPost] 
     public ActionResult Edit(int id, FormCollection collection) 
     { 
      try 
      { 
       // TODO: Add update logic here 

       return RedirectToAction("Index"); 
      } 
      catch 
      { 
       return View(); 
      } 
     } 

     // 
     // GET: /ShowAllReviews/Delete/5 

     public ActionResult Delete(int id) 
     { 
      return View(); 
     } 

     // 
     // POST: /ShowAllReviews/Delete/5 

     [HttpPost] 
     public ActionResult Delete(int id, FormCollection collection) 
     { 
      try 
      { 
       // TODO: Add delete logic here 

       return RedirectToAction("Index"); 
      } 
      catch 
      { 
       return View(); 
      } 
     } 
    } 
} 

は、このサイトが新しい今後のゲーマーのために使用されようとしているとして、私はゲームの外部キーを取得することができているそこの方法であると私はゲームは良いプレーどのようにTOSはレビューのユーザがクリックしたくないしたい数字だけを見てゲーム名は見ません。

あなたが助けてください

EDIT:

は、私が使用し、次の私の見解で:

The ObjectContext instance has been disposed and can no longer be used for operations that require a connection. 

EDIT:しかし、それはcrashsesと、次のエラーが発生します

@Html.DisplayFor(modelItem => item.tblGame.GameName) 

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; } 
    } 
+0

受け取り率が高いほどヘルプが豊富になる場合があります。また、あなたのtblReviewモデルのコードを投稿することができます – glosrob

+0

Okはモデルを投稿します – user1137472

+0

http://stackoverflow.com/questions/5360372/the-objectcontext-instance-has-been-disposed-and-can-no-longer-オペラ用に使用される –

答えて

1

あなたはusing ViewModel classesを検討する必要がありますが、私はあなたが

return View(db.tblReviews.Include("tblGame").ToList()); 

のようなものを使用するようにコントローラを修正する場合は、あなたが上記の投稿したとして、あなたのビューでプロパティを使用することができるだろうと思います。

+0

完璧に働いています。人のコードが間違っているかもしれませんが、なぜshoudl私はちょうど私の%を保つために受け入れるが、感謝が働いた人々がそれを理解していない作品:) – user1137472

関連する問題