Ejemplo n.º 1
0
        public ActionResult Edit(string id, RestaurantPhotoViewModel restaurantPhotoViewModel, HttpPostedFileBase tourism_photo, String oldImg)
        {
            db.Entry(restaurantPhotoViewModel).State = EntityState.Modified;
            db.SaveChanges();

            string fileName = "";

            if (tourism_photo != null)
            {
                if (tourism_photo.ContentLength > 0)
                {
                    System.IO.File.Delete(Server.MapPath("~/images/restaurant/" + oldImg));
                    fileName = System.IO.Path.GetFileName(tourism_photo.FileName);           //取得檔案的檔名(主檔名+副檔名)
                    tourism_photo.SaveAs(Server.MapPath("~/images/restaurant/" + 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"));
        }
Ejemplo n.º 2
0
        public ActionResult Edit(string id)
        {
            RestaurantPhotoViewModel model = new RestaurantPhotoViewModel()
            {
                restaurant        = db.restaurant.Where(m => m.restaurant_id == id).FirstOrDefault(),
                restaurant_photos = db.tourism_photo.Where(m => m.tourism_id == id).ToList()
            };

            return(View(model));
        }
Ejemplo n.º 3
0
        public ActionResult Edit(string id, RestaurantPhotoViewModel restaurantPhotoViewModel, HttpPostedFileBase[] tourism_photo, String oldImg)
        {
            db.Entry(restaurantPhotoViewModel.restaurant).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 = restaurantPhotoViewModel.restaurant.restaurant_id + "_" + DateTime.Now.ToString().Replace("/", "").Replace(":", "").Replace(" ", "") + (i + 1).ToString() + Path.GetExtension(t);
                        if (i < tp1.Count)  //如果原有紀錄
                        {
                            //刪掉原檔案
                            System.IO.File.Delete(Server.MapPath("~/images/restaurant/" + tp1[i].tourism_photo1));
                            tourism_photo[i].SaveAs(Server.MapPath("~/images/restaurant/" + fileName));      //將檔案存到該資料夾
                            //改掉資料庫檔案
                            tp1[i].tourism_photo1 = fileName;
                        }
                        else //如果原本沒有
                        {
                            tourism_photo tp = new tourism_photo();
                            tp.tourism_photo1 = fileName;
                            tourism_photo[i].SaveAs(Server.MapPath("~/images/restaurant/" + fileName));
                            tp.tourism_id = restaurantPhotoViewModel.restaurant.restaurant_id;
                            db.tourism_photo.Add(tp);
                        }
                    }
                }
            }
            db.SaveChanges();

            return(RedirectToAction("Index", new { page = Session["pg"] }));
        }