public ActionResult CreateProduct(CursoASPMVC2.Domain.Product Product) { try { if (ModelState.IsValid) { this.Service.GetEntities().Products.Add(Product); this.Service.GetEntities().SaveChanges(); return RedirectToAction("Products"); } } catch (System.Data.DataException) { ModelState.AddModelError("", "No ha sido posible guardar los datos."); } return View(Product); }
public ActionResult EditProduct(CursoASPMVC2.Domain.Product Product) { try { if (ModelState.IsValid) { CursoASPMVC2.Domain.Product mProduct = this.Service.GetEntities().Products.FirstOrDefault(m => m.ProductId == Product.ProductId); //mProduct = Product; mProduct.Title = Product.Title; mProduct.UnitPrice = Product.UnitPrice; this.Service.GetEntities().SaveChanges(); return RedirectToAction("Products"); } } catch (System.Data.DataException) { ModelState.AddModelError("", "No ha sido posible guardar los datos."); } return View(Product); }
public ActionResult DeleteProduct(CursoASPMVC2.Domain.Product Product) { try { if (ModelState.IsValid) { CursoASPMVC2.Domain.Product mProduct = this.Service.GetEntities().Products.FirstOrDefault(m => m.ProductId == Product.ProductId); this.Service.GetEntities().Products.Remove(mProduct); //Models.Company companyToDelete = new Models.Company() { CompanyId = Company.CompanyId }; //Entities.Entry(companyToDelete).State = System.Data.Entity.EntityState.Deleted; this.Service.GetEntities().SaveChanges(); return RedirectToAction("Products"); } } catch (System.Data.DataException ex) { ModelState.AddModelError("", ex.ToString()); } return View(Product); }