public ActionResult Create(Highlights article) { if (ModelState.IsValid) { int id = GetNextId(); var sourcePath = Path.Combine(Server.MapPath("~/Images"), "temp"); var destPath = Path.Combine(Server.MapPath("~/Images/FeaturedImages"), id.ToString()); var success = MoveTempFeaturedImage(sourcePath, destPath); CreateThumbnail(sourcePath, destPath, success); article.ImageUrl = "~/Images/FeaturedImages/" + id.ToString(); article.LastModifiedDate = DateTime.Now; article.PublishedDate = DateTime.Now; article.Published = false; db.ArticleSet.Add(article); db.SaveChanges(); SendMessage(MessageType.Success, "The highlights successfully created!"); return RedirectToAction("Index"); } SendMessage(MessageType.Error, "Something went wrong! :( Try it again"); return View(article); }
public ActionResult Create(Highlights article) { if (ModelState.IsValid) { int id; try { id = db.ArticleSet.Select(e => e.Id).OrderByDescending(e => e).First() + 1; } catch (Exception) { id = 0; } var sourcePath = Path.Combine(Server.MapPath("~/Images"), "temp"); var destPath = Path.Combine(Server.MapPath("~/Images/FeaturedImages"), id.ToString()); var success = (new UploadController()).SaveFeaturedImage(sourcePath, destPath); if (!success) { sourcePath = Path.Combine(Server.MapPath("~/Images"), "no_image"); System.IO.File.Copy(sourcePath, destPath); } article.ImageUrl = "~/Images/FeaturedImages/" + id.ToString(); article.LastModifiedDate = DateTime.Now; article.PublishedDate = DateTime.Now; article.Published = false; db.ArticleSet.Add(article); db.SaveChanges(); SendMessage(MessageType.Success, "The highlights successfully created!"); return RedirectToAction("Index"); } SendMessage(MessageType.Error, "Something went wrong! :( Try it again"); return View(article); }
public ActionResult Edit(Highlights article) { if (ModelState.IsValid) { var sourcePath = Path.Combine(Server.MapPath("~/Images"), "temp"); var destPath = Path.Combine(Server.MapPath("~/Images/FeaturedImages"), article.Id.ToString()); DeleteCurrentFeaturedImage(sourcePath, destPath); var success = MoveTempFeaturedImage(sourcePath, destPath); if (System.IO.File.Exists(sourcePath)) CreateThumbnail(sourcePath, destPath, success); article.LastModifiedDate = DateTime.Now; db.Entry(article).State = EntityState.Modified; db.SaveChanges(); SendMessage(MessageType.Success, "The highlights successfully modified!"); return RedirectToAction("Index"); } SendMessage(MessageType.Error, "Something went wrong! :( Try it again"); return View(article); }