Esempio n. 1
0
        public ActionResult Create(CrudEventVm model, AddressForEventVm address)
        {
            try
            {
                model.Address       = address;
                model.Event.Address = model.GetAddress(address);
                model.Event.Address.SetCoordinates(address.LatitudeString, address.LongitudeString);

                if (model.Event.Logo == null || model.LogoFile != null)
                {
                    model.Event.Logo = FileUpload.GetBytes(model.LogoFile, "Logo");
                }
                if (model.Event.Cover == null || model.CoverFile != null)
                {
                    model.Event.Cover = FileUpload.GetBytes(model.CoverFile, "Capa");
                }

                ModelState.Remove("Event.Logo");
                ModelState.Remove("Event.Cover");
                if (!ModelState.IsValid)
                {
                    SetBiewBags(model);
                    return(View(model).Error(ModelState));
                }

                _db.Events.Add(model.Event);
                _db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                SetBiewBags(model);
                return(View(model).Error(ex.Message));
            }
        }
Esempio n. 2
0
        public ActionResult Edit(CrudEventVm model, AddressForEventVm address)
        {
            try
            {
                model.Address       = address;
                model.Event.Address = model.GetAddress(address);
                model.Event.Address.SetCoordinates(address.LatitudeString, address.LongitudeString);

                if (model.Event.Logo == null || model.LogoFile != null)
                {
                    model.Event.Logo = FileUpload.GetBytes(model.LogoFile, "Logo");
                }

                if (model.Event.Cover == null || model.CoverFile != null)
                {
                    model.Event.Cover = FileUpload.GetBytes(model.CoverFile, "Capa");
                }

                ModelState.Remove("Event.Logo");
                ModelState.Remove("Event.Cover");
                if (!ModelState.IsValid)
                {
                    SetBiewBags(model);
                    return(View(model).Error(ModelState));
                }

                var oldCompany = _db.Events
                                 .Include(c => c.Address)
                                 //.Include(c => c.Contacts)
                                 .FirstOrDefault(x => x.EventId == model.Event.EventId);
                if (oldCompany == null)
                {
                    return(RedirectToAction("Index").Success("Empresa atualizada com sucesso"));
                }

                // Update parent
                _db.Entry(oldCompany).CurrentValues.SetValues(model.Event);
                oldCompany.Address.UpdateAddress(model.Event.Address);
                _db.Entry(oldCompany.Address).State = EntityState.Modified;
                _db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                SetBiewBags(model);

                return(View(model).Error(ex.Message));
            }
        }