KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Best practice to throw the exception if no entry found in the db? // CONTROLLER public ActionResult Edit(int categoryId, int id) { Product target = Products.GetById(id); if (target == null) throw new HttpException(404, "Product not found"); return View("Edit", target); } // REPOSITORY public Product GetById(int id) { return context.Products.FirstOrDefault(x => x.productId == id); } or // CONTROLLER public ActionResult Edit(int categoryId, int id) { return View("Edit", Products.GetById(id)); } // REPOSITORY public Product GetById(int id) { Product target = context.Products.FirstOrDefault(x => x.productId == id); if (target == null) throw new HttpException(404, "Product not found with given id"); return target; }
Tags (comma-separated)
Save Edits
Cancel