Esempio n. 1
0
        public ActionResult Create(VMEditCreateCity vm)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    City city = new City();
                    city.Name = vm.Name;
                    city.State = db.States.Where(c => c.Id == vm.StateId).FirstOrDefault();
                    this.db.Cities.Add(city);
                    this.db.SaveChanges();
                    this.FlashInfo("Cidade cadastrado com sucesso.");
                    return RedirectToAction("Index");
                }
                catch (DbEntityValidationException ex)
                {
                    foreach(var entityError  in ex.EntityValidationErrors)
                    {
                        foreach(var error in entityError.ValidationErrors)
                        {
                            ModelState.AddModelError(error.PropertyName, error.ErrorMessage);
                        }
                    }
                }catch(Exception ex)
                {
                    ModelState.AddModelError("", "Ocorreu um problema ao salvar a cidade");
                }

            }

            vm.States = db.States.OrderBy(m => m.Name).ToList();
            return View(vm);
        }
Esempio n. 2
0
        public ActionResult Edit(long id, VMEditCreateCity vm)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    City city = this.db.Cities
                                  .Where(c => c.Id == id)
                                  .FirstOrDefault();

                    if (city == null)
                        return HttpNotFound();

                    city.Name = vm.Name;
                    city.UpdatedAt = DateTime.Now;
                    this.db.SaveChanges();
                    this.FlashInfo("Cidade alterada com sucesso.");
                    return RedirectToAction("Index");
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", "Ocorreu um problema ao salvar o estado");
                }

            }
            vm.States = db.States.OrderBy(m => m.Name).ToList();
            return View(vm);
        }
Esempio n. 3
0
 public ActionResult Create()
 {
     VMEditCreateCity vm = new VMEditCreateCity();
     vm.States = db.States.OrderBy(m => m.Name).ToList();
     return View(vm);
 }
Esempio n. 4
0
        public ActionResult Edit(long id)
        {
            City city = this.db.Cities
                                  .Where(c => c.Id == id)
                                  .FirstOrDefault();

            if (city == null)
                return HttpNotFound();

            VMEditCreateCity vm = new VMEditCreateCity();
            vm.States = db.States.OrderBy(m => m.Name).ToList();
            vm.StateId = city.State.Id;
            vm.Name = city.Name;
            vm.Id = city.Id;
            return View(vm);
        }