public ActionResult Edit(Media model, int id, HttpPostedFileBase file) { try { var article = _context.Media.First(e => e.Id == id); TryUpdateModel(article, new[] { "Title", "TitleEn", "TitleUa", "ContentType", "SortOrder", "VideoSrc" }); article.Text = model.Text == null ? "" : HttpUtility.HtmlDecode(model.Text); article.TextEn = model.TextEn == null ? "" : HttpUtility.HtmlDecode(model.TextEn); article.TextUa = model.TextUa == null ? "" : HttpUtility.HtmlDecode(model.TextUa); if (file != null) { if (!string.IsNullOrEmpty(article.ImageSrc)) { ImageHelper.DeleteImage(article.ImageSrc); } string fileName = IOHelper.GetUniqueFileName("~/Content/Images", file.FileName); string filePath = Server.MapPath("~/Content/Images"); filePath = Path.Combine(filePath, fileName); GraphicsHelper.SaveOriginalImageWithDefinedDimentions(filePath, fileName, file, 560, 338, ScaleMode.Crop); article.ImageSrc = fileName; } _context.SaveChanges(); return RedirectToAction("Index"); } catch { return View(); } }
public ActionResult Create(Media model, HttpPostedFileBase file) { try { var media = new Media { Title = model.Title, TitleEn = model.TitleEn, TitleUa = model.TitleUa, ContentType = model.ContentType, SortOrder = model.SortOrder, VideoSrc = model.VideoSrc }; media.Text = model.Text == null ? "" : HttpUtility.HtmlDecode(model.Text); media.TextEn = model.TextEn == null ? "" : HttpUtility.HtmlDecode(model.TextEn); media.TextUa = model.TextUa == null ? "" : HttpUtility.HtmlDecode(model.TextUa); if (file != null) { string fileName = IOHelper.GetUniqueFileName("~/Content/Images", file.FileName); string filePath = Server.MapPath("~/Content/Images"); filePath = Path.Combine(filePath, fileName); GraphicsHelper.SaveOriginalImageWithDefinedDimentions(filePath, fileName, file, 560, 338, ScaleMode.Crop); media.ImageSrc = fileName; } _context.Media.Add(media); _context.SaveChanges(); return RedirectToAction("Index"); } catch { return View(); } }