Ejemplo n.º 1
0
        public ActionResult Create(BlogItem model)
        {
            try
            {
                var blogItem = new BlogItem
                {
                    Name = model.Name.UpdatePageWebName(),
                    Title = model.Title ?? "",
                    TitleEn = model.TitleEn ?? "",
                    Date = model.Date,
                    ShortDescription = model.ShortDescription,
                    ShortDescriptionEn = model.ShortDescriptionEn,
                    Text = model.Text == null ? "" : HttpUtility.HtmlDecode(model.Text),
                    TextEn = model.TextEn == null ? "" : HttpUtility.HtmlDecode(model.TextEn),
                    ShowAsBanner = model.ShowAsBanner
                };

                var file = Request.Files["PreviewImageSrc"];
                if (file != null && !string.IsNullOrEmpty(file.FileName))
                {
                    string fileName = IOHelper.GetUniqueFileName(SiteSettings.BlogPreviewPath, file.FileName);
                    string filePath = Server.MapPath(SiteSettings.BlogPreviewPath);

                    filePath = Path.Combine(filePath, fileName);
                    GraphicsHelper.SaveOriginalImageWithDefinedDimentions(filePath, fileName, file, 247, 247, ScaleMode.Crop);
                    blogItem.PreviewImageSrc = fileName;
                }
                else
                {
                    blogItem.PreviewImageSrc = blogItem.PreviewImageSrc ?? "";
                }

                file = Request.Files["BannerImageSrc"];
                if (file != null && !string.IsNullOrEmpty(file.FileName))
                {
                    string fileName = IOHelper.GetUniqueFileName(SiteSettings.BannersPath, file.FileName);
                    string filePath = Server.MapPath(SiteSettings.BannersPath);

                    filePath = Path.Combine(filePath, fileName);
                    GraphicsHelper.SaveOriginalImageWithDefinedDimentions(filePath, fileName, file, 380, 170, ScaleMode.Crop);
                    blogItem.BannerImageSrc = fileName;
                }
                else
                {
                    blogItem.BannerImageSrc = blogItem.BannerImageSrc ?? "";
                }

                _context.BlogItems.Add(blogItem);
                _context.SaveChanges();

                return RedirectToAction("Index");
            }

            catch (Exception ex)
            {
                TempData["errorMessage"] = !string.IsNullOrEmpty(ex.GetEntityValidationException())
                    ? ex.GetEntityValidationException()
                    : ex.Message;
                return View(model);
            }
        }
Ejemplo n.º 2
0
        public ActionResult Edit(int id, BlogItem model)
        {
            var blogItem = _context.BlogItems.First(a => a.Id == model.Id);
            blogItem.Name = model.Name.UpdatePageWebName();
            blogItem.Title = model.Title ?? "";
            blogItem.TitleEn = model.TitleEn ?? "";
            blogItem.Date = model.Date;
            blogItem.ShortDescription = model.ShortDescription;
            blogItem.ShortDescriptionEn = model.ShortDescriptionEn;
            blogItem.Text = model.Text == null ? "" : HttpUtility.HtmlDecode(model.Text);
            blogItem.TextEn = model.TextEn == null ? "" : HttpUtility.HtmlDecode(model.TextEn);
            blogItem.ShowAsBanner = model.ShowAsBanner;
            var file = Request.Files["PreviewImageSrc"];

            if (file != null && !string.IsNullOrEmpty(file.FileName))
            {
                if (!string.IsNullOrEmpty(blogItem.PreviewImageSrc))
                {
                    ImageHelper.DeleteImage(blogItem.PreviewImageSrc, SiteSettings.BlogPreviewPath);
                }

                string fileName = IOHelper.GetUniqueFileName(SiteSettings.BlogPreviewPath, file.FileName);
                string filePath = Server.MapPath(SiteSettings.BlogPreviewPath);

                filePath = Path.Combine(filePath, fileName);
                GraphicsHelper.SaveOriginalImageWithDefinedDimentions(filePath, fileName, file, 247, 247, ScaleMode.Crop);
                blogItem.PreviewImageSrc = fileName;
            }
            else
            {
                blogItem.PreviewImageSrc = blogItem.PreviewImageSrc ?? "";
            }

            file = Request.Files["BannerImageSrc"];
            if (file != null && !string.IsNullOrEmpty(file.FileName))
            {
                if (!string.IsNullOrEmpty(blogItem.BannerImageSrc))
                {
                    ImageHelper.DeleteImage(blogItem.BannerImageSrc, SiteSettings.BannersPath);
                }

                string fileName = IOHelper.GetUniqueFileName(SiteSettings.BannersPath, file.FileName);
                string filePath = Server.MapPath(SiteSettings.BannersPath);

                filePath = Path.Combine(filePath, fileName);
                GraphicsHelper.SaveOriginalImageWithDefinedDimentions(filePath, fileName, file, 380, 170, ScaleMode.Crop);
                blogItem.BannerImageSrc = fileName;
            }
            else
            {
                blogItem.BannerImageSrc = blogItem.BannerImageSrc ?? "";
            }

            _context.SaveChanges();
            return RedirectToAction("Index");
        }