예제 #1
0
 public ActionResult Create(CarouselVM model)
 {
     if (!ModelState.IsValid) //server side validaiton
     {
         ModelState.AddModelError("", "You have to fill all requiered fields!.");
         return(View(model));
     }
     try
     {
         string fileName = string.Empty;
         if (model.File != null)
         {
             string uploads = Path.Combine(_hosting.WebRootPath, "images/carousels");
             fileName = model.File.FileName;
             string fullPath = Path.Combine(uploads, fileName);
             model.File.CopyTo(new FileStream(fullPath, FileMode.Create));
         }
         Carousel carousel = new Carousel
         {
             Priority = model.Priority,
             ImageUrl = fileName
         };
         this._carouselRepo.Add(carousel);
         return(RedirectToAction(nameof(Index)));
     }
     catch
     {
         return(View());
     }
 }
예제 #2
0
        public ActionResult Edit(int id)
        {
            Carousel carousel = _carouselRepo.Find(id);

            if (carousel == null)
            {
                return(NotFound());
            }
            var vm = new CarouselVM
            {
                CarouselId = id,
                ImgUrl     = carousel.ImageUrl,
                Priority   = carousel.Priority
            };

            return(View(vm));
        }
예제 #3
0
        public ActionResult Edit(int id, CarouselVM model)
        {
            try
            {
                if (!ModelState.IsValid) //server side validaiton
                {
                    ModelState.AddModelError("", "You have to fill all requiered fields!.");
                    return(Edit(model.CarouselId));
                }
                string fileName = string.Empty;
                if (model.File != null)
                {
                    string uploads = Path.Combine(_hosting.WebRootPath, "images/carousels");
                    fileName = model.File.FileName;
                    string fullPath = Path.Combine(uploads, fileName);
                    //delete the old file
                    string oldFileName = this._carouselRepo.Find(id).ImageUrl;
                    string fullOldPath = Path.Combine(uploads, oldFileName);

                    if (fullPath != fullOldPath)
                    {
                        // System.IO.File.Delete(fullOldPath);
                        //save the new file
                        model.File.CopyTo(new FileStream(fullPath, FileMode.Create));
                    }
                }
                Carousel carousel = new Carousel
                {
                    Id       = model.CarouselId,
                    Priority = model.Priority,
                    ImageUrl = fileName
                };
                this._carouselRepo.Update(carousel);

                return(RedirectToAction(nameof(Index)));
            }
            catch (Exception)
            {
                return(NotFound());
            }
        }