Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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);
        }