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();
            }
        }
    }
}

有没有办法让我获得游戏外键,因为这个网站将用于新来的游戏玩家,我想知道哪些游戏很好玩不希望用户点击评论并只看到一个数字而没有游戏名称。

你能帮忙吗

编辑:

在我看来,我使用了以下内容:

@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; }
    }
4

1 回答 1

1

您可能想考虑使用 ViewModel 类,但我认为如果您修改控制器以使用类似

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

正如您在上面发布的那样,您将能够在您的视图中使用该属性。

于 2012-04-07T23:37:10.477 回答