Пример #1
0
 public ActionResult Create()
 {
     var model = new ReUzze.Models.AddressViewModel
     {
         Address = new Address(),
         Regions = new SelectList(this.UnitOfWork.RegionRepository.Get(), "Id", "Name")
     };
     return View(model);
 }
Пример #2
0
        public ActionResult Create(ReUzze.Models.AddressViewModel viewModel)
        {
            try
            {
                if (!ModelState.IsValid)
                    throw new Exception();

                var regions = this.UnitOfWork.RegionRepository.Get().Count();
                if (regions == 0)
                {
                    var model = new ReUzze.Models.AddressViewModel
                    {
                        StatusMessage = "There are no regions in the database ...",
                        Regions = new SelectList(this.UnitOfWork.RegionRepository.Get(), "Id", "Name")
                    };
                    return View(model);
                }

                if (viewModel.SelectRegionId == 0)
                {
                    var model = new ReUzze.Models.AddressViewModel
                    {
                        StatusMessage = "You need to select a region!",
                        Regions = new SelectList(this.UnitOfWork.RegionRepository.Get(), "Id", "Name")
                    };
                    return View(model);
                }

                viewModel.Address.Latitude = Convert.ToDecimal(viewModel.Address.Latitude);
                viewModel.Address.Longitude = Convert.ToDecimal(viewModel.Address.Longitude);

                viewModel.Address.RegionId = viewModel.SelectRegionId;
                this.UnitOfWork.AddressRepository.Insert(viewModel.Address);
                this.UnitOfWork.Save();

                return RedirectToAction("Index", "Address");
            }
            catch
            {
                var model = new ReUzze.Models.AddressViewModel
                {
                    Address = viewModel.Address,
                    Regions = new SelectList(this.UnitOfWork.RegionRepository.Get(), "Id", "Name")
                };
                return View(model);
            }
        }
Пример #3
0
 public ActionResult Edit(Int32 id)
 {
     try
     {
         var address = this.UnitOfWork.AddressRepository.GetByID(id);
         var model = new ReUzze.Models.AddressViewModel
         {
             Address = address,
             Regions = new SelectList(this.UnitOfWork.RegionRepository.Get(), "Id", "Name"),
             SelectRegionId = address.RegionId
         };
         return View(model);
     }
     catch
     {
         return RedirectToAction("Index", "Address");
     }
 }
Пример #4
0
        public ActionResult Edit(ReUzze.Models.AddressViewModel viewModel)
        {
            try
            {
                if (!ModelState.IsValid)
                    return View(viewModel);

                var originalAddress = this.UnitOfWork.AddressRepository.GetByID(viewModel.Address.Id);

                if (originalAddress == null)
                    throw new Exception();

                originalAddress.City = viewModel.Address.City;
                originalAddress.Street = viewModel.Address.Street;
                originalAddress.StreetNumber = viewModel.Address.StreetNumber;
                originalAddress.RegionId = viewModel.SelectRegionId;

                this.UnitOfWork.AddressRepository.Update(originalAddress);
                this.UnitOfWork.Save();

                return RedirectToAction("Index", "Address");
            }
            catch
            {
                var model = new ReUzze.Models.AddressViewModel
                {
                    Address = viewModel.Address,
                    Regions = new SelectList(this.UnitOfWork.RegionRepository.Get(), "Id", "Name")
                };
                return View(model);
            }
        }