Beispiel #1
0
        public ActionResult Edit(string id, SpotPhotoViewModel spotPhotoViewModel, HttpPostedFileBase tourism_photo, String oldImg)
        {
            db.Entry(spotPhotoViewModel).State = EntityState.Modified;
            db.SaveChanges();

            string fileName = "";

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

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

                    //}
                }
                //else //if(String.IsNullOrEmpty(oldImg))
                //{
                //    tourism_photo tp = new tourism_photo();
                //    tp.tourism_photo1 = fileName;
                //    tp.tourism_id = spotPhotoViewModel.spot.spot_id;
                //    db.tourism_photo.Add(tp);
                //}
            }
            db.SaveChanges();
            return(RedirectToAction("Index", new { page = Session["pg"] }));
        }