コード例 #1
0
        public ActionResult Create(MainBanner model)
        {
            try
            {
                var mainBanner = new MainBanner
                {
                    Title = model.Title ?? "", 
                    TitleEn = model.TitleEn ?? "", 
                    TitleUa = model.TitleUa ?? "", 
                    Description = model.Description ?? "",
                    DescriptionEn = model.DescriptionEn ?? "",
                    DescriptionUa = model.DescriptionUa ?? ""
                };

                var file = Request.Files[0];
                if (file != null && !string.IsNullOrEmpty(file.FileName))
                {
                    string fileName = IOHelper.GetUniqueFileName("~/Content/Images", file.FileName);
                    string filePath = Server.MapPath("~/Content/Images");

                    filePath = Path.Combine(filePath, fileName);
                    GraphicsHelper.SaveOriginalImage(filePath, fileName, file, 1500);
                    mainBanner.ImageSrc = fileName;
                }
                else
                {
                    mainBanner.ImageSrc = mainBanner.ImageSrc ?? "";
                }

                _context.MainBanners.Add(mainBanner);
                _context.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
コード例 #2
0
        public ActionResult Edit(int id, MainBanner model)
        {
            try
            {
                var mainBanner = _context.MainBanners.First(b => b.Id == id);
                mainBanner.Title = model.Title ?? "";
                mainBanner.TitleEn = model.TitleEn ?? "";
                mainBanner.TitleUa = model.TitleUa ?? "";
                mainBanner.Description = model.Description ?? "";
                mainBanner.DescriptionEn = model.DescriptionEn ?? "";
                mainBanner.DescriptionUa = model.DescriptionUa ?? "";

                var file = Request.Files[0];
                if (file != null && !string.IsNullOrEmpty(file.FileName))
                {
                    if (!string.IsNullOrEmpty(mainBanner.ImageSrc))
                    {
                        ImageHelper.DeleteImage(mainBanner.ImageSrc);
                    }

                    string fileName = IOHelper.GetUniqueFileName("~/Content/Images", file.FileName);
                    string filePath = Server.MapPath("~/Content/Images");

                    filePath = Path.Combine(filePath, fileName);
                    GraphicsHelper.SaveOriginalImage(filePath, fileName, file, 1500);
                    mainBanner.ImageSrc = fileName;
                }
                else
                {
                    mainBanner.ImageSrc = mainBanner.ImageSrc ?? "";
                }

                _context.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }