Пример #1
0
        public ActionResult Create(VMEditCreateState vm)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    State state = new State();
                    state.Name = vm.Name;
                    state.Code = vm.Code;
                    state.Country = db.Countries.Where(c => c.Id == vm.CountryId).FirstOrDefault();
                    this.db.States.Add(state);
                    this.db.SaveChanges();
                    this.FlashInfo("Estado 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 o estado");
                }

            }

            vm.Countries = db.Countries.OrderBy(m => m.Name).ToList();
            return View(vm);
        }
Пример #2
0
        public ActionResult Edit(long id, VMEditCreateState vm)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    State state = this.db.States
                                  .Where(c => c.Id == id)
                                  .FirstOrDefault();

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

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

            }
            vm.Countries = db.Countries.OrderBy(m => m.Name).ToList();
            return View(vm);
        }
Пример #3
0
 public ActionResult Create()
 {
     VMEditCreateState vm = new VMEditCreateState();
     vm.Countries = db.Countries.OrderBy(m => m.Name).ToList();
     return View(vm);
 }
Пример #4
0
        public ActionResult Edit(long id)
        {
            State state = this.db.States
                                  .Where(c => c.Id == id)
                                  .FirstOrDefault();

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

            VMEditCreateState vm = new VMEditCreateState();
            vm.Countries = db.Countries.OrderBy(m => m.Name).ToList();
            vm.CountryId = state.Country.Id;
            vm.Name = state.Name;
            vm.Id = state.Id;
            vm.Code = state.Code;
            return View(vm);
        }