Hi people am new to this website am hoping you can help me
I want a way in which I can display the value of the Foreign key which in my case is Game and instead of the Game Number i want to display the name then displaying a number. For example I have the following view to show all reviews:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
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();
}
}
}
}
is the right idea, but you need to do this from your controller and populate a view model with it.The view model is what you then pass to the view, not your domain/entity model.
Read up on the www.asp.net/mvc tutorials for the concept and examples of using view models.
Sounds like you are using lazyloading in the db context, if you create it in a using block it will be disposed before all the data is pulled in.
For web apps where there is a distinct disconnect, I like to turn lazyloading off and make sure that all my entities are populated ready for display in the views.
TommyGun123
0 Points
1 Post
Foreign Key Just showing a number which is associated with the primary key
Apr 07, 2012 11:34 PM|LINK
Hi people am new to this website am hoping you can help me
I want a way in which I can display the value of the Foreign key which in my case is Game and instead of the Game Number i want to display the name then displaying a number. For example I have the following view to show all reviews:
using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; 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(); } } } }I have tried the following:
But i get this error:C mvc3
BrockAllen
All-Star
27534 Points
4907 Posts
MVP
Re: Foreign Key Just showing a number which is associated with the primary key
Apr 07, 2012 11:48 PM|LINK
is the right idea, but you need to do this from your controller and populate a view model with it.The view model is what you then pass to the view, not your domain/entity model.
Read up on the www.asp.net/mvc tutorials for the concept and examples of using view models.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
Slicksim
Participant
1925 Points
350 Posts
Re: Foreign Key Just showing a number which is associated with the primary key
Apr 11, 2012 07:40 PM|LINK
Hi,
Sounds like you are using lazyloading in the db context, if you create it in a using block it will be disposed before all the data is pulled in.
For web apps where there is a distinct disconnect, I like to turn lazyloading off and make sure that all my entities are populated ready for display in the views.
Si