public void InsertOrUpdate(Product products) { if (products.Id == default(int)) { // New entity context.Products.Add(products); } else { // Existing entity context.Products.Attach(products); context.Entry(products).State = EntityState.Modified; } }
public ActionResult Create(Product products) { if (ModelState.IsValid) { productsRepository.InsertOrUpdate(products); productsRepository.Save(); return RedirectToAction("Index"); } else { ViewBag.PossibleCategories = categoriesRepository.All; return View(); } }