public ActionResult Create(Customer customer) { try { if (ModelState.IsValid) { DbContext.Customers.Add(customer); if (DbContext.SaveChanges() > 0) return Redirect(null); ModelState.AddModelError("", "Đã có lỗi xảy ra. Vui lòng thử lại sau."); } } catch (Exception ex) { ModelState.AddModelError("", ex.Message); ex.Write(LogPath); } return View(customer); }
public ActionResult Edit(Customer customer) { var customerDb = DbContext.Customers.Find(customer.CustomerId); if (customerDb == null) { ModelState.AddModelError(string.Empty, "Không thể lưu thay đổi, danh mục này đã bị xóa"); } if (ModelState.IsValid) { try { TryUpdateModel(customerDb); DbContext.Entry(customerDb).State = EntityState.Modified; if (DbContext.SaveChanges() > 0) { return Redirect(null); } } catch (Exception ex) { ModelState.AddModelError("", ex.Message); ex.Write(LogPath); } } return View(customerDb); }