public ActionResult Create(Category category) { if (ModelState.IsValid) { try { db.Categories.Add(category); db.SaveChanges(); return RedirectToAction("Index"); } catch (DbEntityValidationException ex) { // see: PostController.Create (POST) var errors = ex.EntityValidationErrors.First().ValidationErrors.First(); this.ModelState.AddModelError(errors.PropertyName, errors.ErrorMessage); } } return View(category); }
public ActionResult Edit(Category category) { if (ModelState.IsValid) { try { db.Entry(category).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } catch (DbEntityValidationException ex) { var errors = ex.EntityValidationErrors.First().ValidationErrors.First(); this.ModelState.AddModelError(errors.PropertyName, errors.ErrorMessage); } } return View(category); }