The primary key is a string (nvarchar) and I’m not sure how to identify a record in this instance.
I cannot use the following as it is not an integer:
public ActionResult Details(int Id = 0) { return View(db.Consultants.Find(Id)); }
Thx
Hi,
The key can be any type, not only "Id".
Here an example:
Controller:
using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Linq; using System.Net; using System.Web; using System.Web.Mvc; using PersonEntity.Model; namespace EstateAgency.Admin.Controllers { public class ProductPhotoSetsController : Controller { private PersonEntityDbContext db = new PersonEntityDbContext(); // GET: ProductPhotoSets public ActionResult Index() { var productPhotoSets = db.ProductPhotoSets.Include(p => p.Product).Include(p => p.ProductPhoto); return View(productPhotoSets.ToList()); } // GET: ProductPhotoSets/Details/5 public ActionResult Details(int? productId, int? photoId) { if (productId == null && photoId == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } int idProduct = Convert.ToInt32(productId); int idPhoto = Convert.ToInt32(photoId); ProductPhotoSet productPhotoSet = db.ProductPhotoSets.Where(p => p.ProductId == idProduct).Where(q => q.PhotoId == idPhoto).SingleOrDefault(); if (productPhotoSet == null) { return HttpNotFound(); } return View(productPhotoSet); } // GET: ProductPhotoSets/Create public ActionResult Create() { ProductPhotoSet productPhotoSet = new ProductPhotoSet(); productPhotoSet.ModifiedDate = DateTime.Now; ViewBag.ProductId = new SelectList(db.Products, "Id", "Name"); ViewBag.PhotoId = new SelectList(db.ProductPhotoes, "Id", "PhotoFileName"); return View(productPhotoSet); } // POST: ProductPhotoSets/Create // To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create([Bind(Include = "ProductId,PhotoId,Title,Caption,Primary,ModifiedDate")] ProductPhotoSet productPhotoSet) { productPhotoSet.ModifiedDate = DateTime.Now; if (ModelState.IsValid) { db.ProductPhotoSets.Add(productPhotoSet); db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.ProductId = new SelectList(db.Products, "Id", "Name", productPhotoSet.ProductId); ViewBag.PhotoId = new SelectList(db.ProductPhotoes, "Id", "PhotoFileName", productPhotoSet.PhotoId); return View(productPhotoSet); } // GET: ProductPhotoSets/Edit/5 public ActionResult Edit(int? productId, int? photoId) { if (productId == null && photoId == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } int idProduct = Convert.ToInt32(productId); int idPhoto = Convert.ToInt32(photoId); ProductPhotoSet productPhotoSet = db.ProductPhotoSets.Where(p => p.ProductId == idProduct).Where(q => q.PhotoId == idPhoto).SingleOrDefault(); if (productPhotoSet == null) { return HttpNotFound(); } ViewBag.ProductId = new SelectList(db.Products, "Id", "Name", productPhotoSet.ProductId); ViewBag.PhotoId = new SelectList(db.ProductPhotoes, "Id", "PhotoFileName", productPhotoSet.PhotoId); return View(productPhotoSet); } // POST: ProductPhotoSets/Edit/5 // To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit([Bind(Include = "ProductId,PhotoId,Title,Caption,Primary,ModifiedDate")] ProductPhotoSet productPhotoSet) { productPhotoSet.ModifiedDate = DateTime.Now; if (ModelState.IsValid) { db.Entry(productPhotoSet).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.ProductId = new SelectList(db.Products, "Id", "Name", productPhotoSet.ProductId); ViewBag.PhotoId = new SelectList(db.ProductPhotoes, "Id", "PhotoFileName", productPhotoSet.PhotoId); return View(productPhotoSet); } // GET: ProductPhotoSets/Delete/5 public ActionResult Delete(int? productId, int? photoId) { if (productId == null && photoId == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } int idProduct = Convert.ToInt32(productId); int idPhoto = Convert.ToInt32(photoId); ProductPhotoSet productPhotoSet = db.ProductPhotoSets.Where(p => p.ProductId == idProduct).Where(q => q.PhotoId == idPhoto).SingleOrDefault(); if (productPhotoSet == null) { return HttpNotFound(); } return View(productPhotoSet); } // POST: ProductPhotoSets/Delete/5 [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public ActionResult DeleteConfirmed(int productId, int photoId) { if (productId == null && photoId == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } ProductPhotoSet productPhotoSet = db.ProductPhotoSets.Where(p => p.ProductId == productId).Where(q => q.PhotoId == photoId).SingleOrDefault(); db.ProductPhotoSets.Remove(productPhotoSet); db.SaveChanges(); return RedirectToAction("Index"); } protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); } } }
Is called at the Index.cshtml:
<td> @Html.ActionLink("Edit", "Edit", new { ProductId = item.ProductId, PhotoId = item.PhotoId }) | @Html.ActionLink("Details", "Details", new { ProductId = item.ProductId, PhotoId = item.PhotoId }) | @Html.ActionLink("Delete", "Delete", new { ProductId = item.ProductId, PhotoId = item.PhotoId }) </td>
The point is under the @Html.ActionLink at this part:
new { ProductId = item.ProductId, PhotoId = item.PhotoId }
In your case can be:
@Html.ActionLink("Details", "Details", new { YourPrimaryKey = item.YourPrimaryKey})
And at the controller:
public ActionResult Details(string yourPrimaryKey) { if (yourPrimaryKey == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } return View(yourModel); }
Hope can help, have fun.
Thank you sooo much.