Ejemplo n.º 1
0
        public ActionResult Address(int id = 0)
        {
            ViewBag.Edit = id != 0;

            List <CityType> types;

            using (var repository = new NHGetAllRepository <CityType>())
            {
                types = repository.GetAll().ToList();
            }

            var list = (IEnumerable <SelectListItem>) new SelectList(types, "id", "Name");

            AddressDto address = new AddressDto();

            if (id != 0)
            {
                using (var repository = new NHRepository <Address>())
                {
                    var t = repository.Get(id);
                    address = new AddressDto()
                    {
                        Id       = t.Id,
                        House    = t.House,
                        Street   = t.Street.Name,
                        Country  = t.Country.Name,
                        Region   = t.Region.Name,
                        CityType = t.CityType.Id,
                        City     = t.City.Name
                    };
                }


                list = types.Select(x =>
                                    new SelectListItem()
                {
                    Text = x.Name, Selected = x.Id == address.CityType, Value = x.Id.ToString()
                });
            }

            ViewData["CityType"] = list;

            return(View(address));
        }
Ejemplo n.º 2
0
        public ActionResult Address(AddressDto address)
        {
            List <CityType> types;

            using (var repository = new NHGetAllRepository <CityType>())
            {
                types = repository.GetAll().ToList();
            }

            var list = (IEnumerable <SelectListItem>) new SelectList(types, "Id", "Name");

            ViewData["CityType"] = list;
            ViewBag.Edit         = address.Id != 0;


            if (address.Country.IsNullOrEmpty())
            {
                ModelState.AddModelError("Country", "Поле не может быть пустым");
            }
            if (address.Region.IsNullOrEmpty())
            {
                ModelState.AddModelError("Region", "Поле не может быть пустым");
            }
            if (address.City.IsNullOrEmpty())
            {
                ModelState.AddModelError("City", "Поле не может быть пустым");
            }
            if (address.CityType == 0)
            {
                ModelState.AddModelError("CityType", "Поле не может быть пустым");
            }
            if (address.Street.IsNullOrEmpty())
            {
                ModelState.AddModelError("Street", "Поле не может быть пустым");
            }
            if (address.House.IsNullOrEmpty())
            {
                ModelState.AddModelError("House", "Поле не может быть пустым");
            }

            if (!ModelState.IsValid)
            {
                return(View(address));
            }


            var session = NHibernateHelper.OpenSession();


            using (var tx = session.BeginTransaction())
            {
                var country = session.Query <Country>().FirstOrDefault(x => x.Name == address.Country) ??
                              new Country()
                {
                    Name = address.Country
                };

                var region = session.Query <Region>().FirstOrDefault(x => x.Name == address.Region) ??
                             new Region()
                {
                    Name = address.Region, Country = country
                };

                var cityType = session.Query <CityType>().First(x => x.Id == address.CityType);

                var city = session.Query <City>().FirstOrDefault(x => x.Name == address.City) ??
                           new City()
                {
                    Name = address.City, Region = region, CityType = cityType
                };

                var street = session.Query <Street>().FirstOrDefault(x => x.Name == address.Street) ??
                             new Street()
                {
                    Name = address.Street, City = city
                };

                var entity = new Address()
                {
                    Id     = address.Id,
                    House  = address.House,
                    Street = street,
                };

                session.SaveOrUpdate(entity);

                tx.Commit();
            }

            return(RedirectToAction("Index"));
        }