public ActionResult Show(int id) { var article = articlesRepository.GetById(id); var viewModel = new ArticleViewModel { Id = article.Id, Title = article.Title, Text = article.Text, ArticleImagePath = article.File.FileName }; return View(viewModel); }
public ActionResult EditNew(ArticleViewModel article, HttpPostedFileBase file) { if (ModelState.IsValid) { using (new UnitOfWork(_currentContext)) { var existingArticle = articlesRepository.GetById(article.Id); if (file != null) { //upload file first var ticks = DateTime.Now.Ticks; var fileName = String.Format("slideshow-{0}", ticks) + Path.GetExtension(file.FileName); var absoluteTempPath = Server.MapPath("~/Content/images/uploads/temp/"); var absolutePath = Server.MapPath("~/Content/images/uploads/news/"); var fullVirtualPath = "~/Content/images/uploads/news/" + fileName; //create a temp file first, then compress it FileUploader.UploadFile(file, fileName, absoluteTempPath); var encoder = new ImageEncoder(); encoder.Compress(absoluteTempPath + fileName, absolutePath + fileName, 620, 465); //after compressing deleting original file FileUploader.DeleteFile(absoluteTempPath + fileName); var articleImage = new File() { FileName = fullVirtualPath, FilePurposeId = (int)FilePurposes.Slideshow }; //delete the old file FileUploader.DeleteFile(Server.MapPath(existingArticle.File.FileName)); filesRepository.Remove(existingArticle.FileId); existingArticle.File = articleImage; } existingArticle.Text = article.Text; existingArticle.Title = article.Title; existingArticle.PostedAt = DateTime.Now; existingArticle.Account = accountRepository.GetByUsername(CurrentUserName); } TempData[Const.ActionResultInfo] = "Новость успешно отредактирована"; return RedirectToAction("News"); } TempData[Const.ActionErrorInfo] = "Невозможно отредактировать новость, проверьте правильность заполнения полей"; return View(); }
public ActionResult EditNew(int id) { var article = articlesRepository.GetById(id); var model = new ArticleViewModel() { Id = id, Text = article.Text, Title = article.Title, ArticleImagePath = article.File.FileName }; return View(model); }