Exemplo n.º 1
0
        public ActionResult Edit(string id, HotelPhotoViewModel hotelPhotoViewModel, HttpPostedFileBase tourism_photo, String oldImg)
        {
            db.Entry(hotelPhotoViewModel.hotel).State = EntityState.Modified;
            db.SaveChanges();

            string fileName = "";

            if (tourism_photo != null)
            {
                if (tourism_photo.ContentLength > 0)
                {
                    System.IO.File.Delete(Server.MapPath("~/images/hotel/" + oldImg));
                    fileName = System.IO.Path.GetFileName(tourism_photo.FileName);      //取得檔案的檔名(主檔名+副檔名)
                    tourism_photo.SaveAs(Server.MapPath("~/images/hotel/" + fileName)); //將檔案存到該資料夾
                }
            }
            else
            {
                fileName = oldImg;
            }
            var tp1 = db.tourism_photo.Where(m => m.tourism_id == id).FirstOrDefault();

            tp1.tourism_photo1 = fileName;
            db.SaveChanges();

            return(RedirectToAction("Index"));
        }
Exemplo n.º 2
0
        public ActionResult Edit(string id)
        {
            HotelPhotoViewModel model = new HotelPhotoViewModel()
            {
                hotel        = db.hotels.Where(m => m.hotel_id == id).FirstOrDefault(),
                hotel_photos = db.tourism_photo.Where(m => m.tourism_id == id).FirstOrDefault()
            };

            return(View(model));
        }
Exemplo n.º 3
0
        public ActionResult Edit(string id, HotelPhotoViewModel hotelPhotoViewModel, HttpPostedFileBase[] tourism_photo, String oldImg)
        {
            db.Entry(hotelPhotoViewModel.hotel).State = EntityState.Modified;
            db.SaveChanges();

            //圖片
            string fileName = "";
            var    tp1      = db.tourism_photo.Where(m => m.tourism_id == id).ToList();

            for (int i = 0; i < 3; i++)
            {
                if (tourism_photo[i] != null)
                {
                    //如果有新增檔案到input
                    if (tourism_photo[i].ContentLength > 0)
                    {
                        //改名
                        string t = tourism_photo[i].FileName;
                        fileName = hotelPhotoViewModel.hotel.hotel_id + "_" + DateTime.Now.ToString().Replace("/", "").Replace(":", "").Replace(" ", "") + (i + 1).ToString() + Path.GetExtension(t);
                        if (i < tp1.Count)  //如果原有紀錄
                        {
                            //刪掉原檔案
                            System.IO.File.Delete(Server.MapPath("~/images/hotel/" + tp1[i].tourism_photo1));
                            tourism_photo[i].SaveAs(Server.MapPath("~/images/hotel/" + fileName));      //將檔案存到該資料夾
                            //改掉資料庫檔案
                            tp1[i].tourism_photo1 = fileName;
                        }
                        else //如果原本沒有
                        {
                            tourism_photo tp = new tourism_photo();
                            tp.tourism_photo1 = fileName;
                            tourism_photo[i].SaveAs(Server.MapPath("~/images/hotel/" + fileName));
                            tp.tourism_id = hotelPhotoViewModel.hotel.hotel_id;
                            db.tourism_photo.Add(tp);
                        }
                    }
                }
            }
            db.SaveChanges();
            return(RedirectToAction("Index", new { page = Session["pg"] }));
        }