Example #1
0
        public async Task<ActionResult> Index(int page = 1)
        {
            var entities = await hotelRep.Get().ToListAsync();

            var models = new List<HotelViewModel>();

            HotelViewModel model;

            foreach (var h in entities)
            {
                model = new HotelViewModel();
                model.Id = h.Id;
                model.Name = h.Name;
                model.RegionId = h.RegionId;
                model.RegionName = RegionToName(h.RegionId, "");
                model.Phone = h.Phone;
                model.Created = h.Created;
                model.Modified = h.Modified;
                model.Status = (HotelStatus)h.Status;
                var hotelImg = imgRep.FindById(h.Id, 0);
                if (hotelImg != null)
                {
                    model.ImgUrl = hotelImg.Url;
                }
                models.Add(model);
            }

            return View(models.ToPagedList(page, 10));
        }
Example #2
0
        public async Task<ActionResult> Create(HotelViewModel model, FormCollection fc)
        {
            if (ModelState.IsValid)
            {
                var hotel = hotelRep.Create(new Hotel
                {
                    Name = model.Name,
                    RegionId = model.RegionId,
                    Address = model.Address,
                    Phone = model.Phone,
                    Description = model.Description,
                    Created = DateTime.Now,
                    Modified = DateTime.Now,
                    Status = 1
                });

                string[] imgUrl = fc["ImgUrl"].Split(',');

                for (int i = 0; i < imgUrl.Length; i++)
                {
                    imgRep.Create(new HotelImg
                    {
                        HotelId = model.Id,
                        Position = i,
                        Url = imgUrl[i]
                    });
                }

                await uow.SaveAsync();
                return RedirectToAction("Index");
            }
            ModelState.AddModelError("", "操作失败。");

            var provs = await regionRep.Get(r => r.ParentId == 1).ToListAsync();
            ViewData["ProvSelectList"] = new SelectList(provs, "RegionId", "RegionName", 440000);

            var cities = await regionRep.Get(r => r.ParentId == 440000).ToListAsync();
            cities.Insert(0, new Region { RegionId = 0, RegionName = "--请选择--" });
            ViewData["CitySelectList"] = new SelectList(cities, "RegionId", "RegionName");
            return View(model);
        }
Example #3
0
        public async Task<ActionResult> Edit(HotelViewModel model, FormCollection fc)
        {
            if (ModelState.IsValid)
            {
                var hotel = hotelRep.FindById(model.Id);
                if (hotel == null)
                {
                    return HttpNotFound();
                }

                hotel.Name = model.Name;
                hotel.RegionId = model.RegionId;
                hotel.Address = model.Address;
                hotel.Phone = model.Phone;
                hotel.Description = model.Description;
                hotel.Modified = DateTime.Now;
                hotelRep.Update(hotel);

                imgRep.Delete(i => i.HotelId == model.Id);

                string[] imgUrl = fc["ImgUrl"].Split(',');

                for (int i = 0; i < imgUrl.Length; i++)
                {
                    imgRep.Create(new HotelImg
                    {
                        HotelId = model.Id,
                        Position = i,
                        Url = imgUrl[i]
                    });
                }

                await uow.SaveAsync();
                return RedirectToAction("Index");
            }
            ModelState.AddModelError("", "操作失败。");

            var provs = regionRep.Get(r => r.ParentId == 1).ToList();
            ViewData["ProvSelectList"] = new SelectList(provs, "RegionId", "RegionName", model.ProvinceId);

            var provId = regionRep.FindById(model.RegionId).ParentId;
            var cities = regionRep.Get(r => r.ParentId == provId).ToList();
            ViewData["CitySelectList"] = new SelectList(cities, "RegionId", "RegionName", model.RegionId);
            return View(model);
        }