コード例 #1
0
        public ActionResult Create(Article model)
        {
            try
            {
                var article = new Article
                {
                    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.ArticlePreviewPath, file.FileName);
                    string filePath = Server.MapPath(SiteSettings.ArticlePreviewPath);

                    filePath = Path.Combine(filePath, fileName);
                    GraphicsHelper.SaveOriginalImageWithDefinedDimentions(filePath, fileName, file, 238, 237, ScaleMode.Crop);
                    article.PreviewImageSrc = fileName;
                }
                else
                {
                    article.PreviewImageSrc = article.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);
                    article.BannerImageSrc = fileName;
                }
                else
                {
                    article.BannerImageSrc = article.BannerImageSrc ?? "";
                }

                _context.Articles.Add(article);
                _context.SaveChanges();

                return RedirectToAction("Index");
            }

            catch (Exception ex)
            {
                TempData["errorMessage"] = !string.IsNullOrEmpty(ex.GetEntityValidationException())
                    ? ex.GetEntityValidationException()
                    : ex.Message;
                return View(model);
            }
        }
コード例 #2
0
        public ActionResult Edit(int id, Article model)
        {
            var article = _context.Articles.First(a => a.Id == model.Id);
            article.Name = model.Name.UpdatePageWebName();
            article.Title = model.Title ?? "";
            article.TitleEn = model.TitleEn ?? "";
            article.Date = model.Date;
            article.ShortDescription = model.ShortDescription;
            article.ShortDescriptionEn = model.ShortDescriptionEn;
            article.Text = model.Text == null ? "" : HttpUtility.HtmlDecode(model.Text);
            article.TextEn = model.TextEn == null ? "" : HttpUtility.HtmlDecode(model.TextEn);
            article.ShowAsBanner = model.ShowAsBanner;

            var file = Request.Files["PreviewImageSrc"];
            if (file != null && !string.IsNullOrEmpty(file.FileName))
            {
                if (!string.IsNullOrEmpty(article.PreviewImageSrc))
                {
                    ImageHelper.DeleteImage(article.PreviewImageSrc, SiteSettings.ArticlePreviewPath);
                }

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

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

            file = Request.Files["BannerImageSrc"];
            if (file != null && !string.IsNullOrEmpty(file.FileName))
            {
                if (!string.IsNullOrEmpty(article.BannerImageSrc))
                {
                    ImageHelper.DeleteImage(article.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);
                article.BannerImageSrc = fileName;
            }
            else
            {
                article.BannerImageSrc = article.BannerImageSrc ?? "";
            }

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