// GET: Makalah/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Makalah makalah = db.Makalah.Find(id); if (makalah == null) { return(HttpNotFound()); } EditMakalahViewModel editMakalahViewModel = new EditMakalahViewModel { Id = makalah.Id, CategoryId = makalah.CategoryId, Title = makalah.Title, Abstract = makalah.Abstract, AbstractFileName = makalah.AbstractFileName, FullPaperFileName = makalah.FullPaperFileName, Description = makalah.Description }; return(View(editMakalahViewModel)); }
public ActionResult Edit(EditMakalahViewModel model) { Makalah makalah = db.Makalah.Find(model.Id); if (makalah == null) { return(HttpNotFound()); } if (!ModelState.IsValid) { return(View(model)); } int titleWordsCount = GetWordsCount(model.Title); if (titleWordsCount > GlobalData.TitleMaxWordsCount) { ModelState.AddModelError("", string.Format("Jumlah kata judul anda {0}. Maksimal untuk jumlah kata untuk judul adalah {1}.", titleWordsCount, GlobalData.TitleMaxWordsCount)); return(View(model)); } int abstractWordsCount = GetWordsCount(model.Abstract); if (abstractWordsCount > GlobalData.AbstractMaxWordsCount) { ModelState.AddModelError("", string.Format("Jumlah kata abstrak anda {0}. Maksimal untuk jumlah kata untuk abstrak adalah {1}.", abstractWordsCount, GlobalData.AbstractMaxWordsCount)); return(View(model)); } bool includeNewAbstractFile = false; if (model.AbstractFileUpload != null && model.AbstractFileUpload.ContentLength > 0) { if (model.AbstractFileUpload.ContentType != "application/pdf") { ModelState.AddModelError("", "Format file abstrak yang diunggah harus bertipe pdf."); return(View("Edit", model)); } if (model.AbstractFileUpload.ContentLength > MaxUploadFileSize) { ModelState.AddModelError("", string.Format("Maksimal ukuran file abstrak adalah {0} bytes (1 MB)", MaxUploadFileSize)); return(View("Edit", model)); } includeNewAbstractFile = true; } bool includeNewFullPaper = false; if (model.FullPaperFileUpload != null && model.FullPaperFileUpload.ContentLength > 0) { if (model.FullPaperFileUpload.ContentType != "application/pdf") { ModelState.AddModelError("", "Format file full paper yang diunggah harus bertipe pdf."); return(View("Edit", model)); } if (model.FullPaperFileUpload.ContentLength > MaxUploadFileSize) { ModelState.AddModelError("", string.Format("Maksimal ukuran file full paper adalah {0} bytes (1 MB)", MaxUploadFileSize)); return(View("Edit", model)); } includeNewFullPaper = true; } try { string newAbstractFileName = makalah.AbstractFileName; if (includeNewAbstractFile) { newAbstractFileName = GetFileNameFromDateTime(DateTime.UtcNow, "Abs", ".pdf"); string abstractPath = Server.MapPath(GlobalData.AbstractFileSavePath); if (!Directory.Exists(abstractPath)) { Directory.CreateDirectory(abstractPath); } string abstractSaveFilePath = Path.Combine(abstractPath, newAbstractFileName); model.AbstractFileUpload.SaveAs(abstractSaveFilePath); //hapus file yang lama if (!string.IsNullOrEmpty(makalah.AbstractFileName)) { string oldFileName = makalah.AbstractFileName; FileInfo oldAbstract = new FileInfo(Path.Combine(abstractPath, oldFileName)); if (oldAbstract.Exists) { oldAbstract.Delete(); } } } makalah.CategoryId = model.CategoryId; makalah.Title = model.Title; makalah.Abstract = model.Abstract; makalah.AbstractFileName = newAbstractFileName; makalah.Description = model.Description; makalah.PaperAbstractStatus = EPaperAbstractStatus.Edit; makalah.PaperAbstractConfirmMessage = GlobalData.GetPaperAbstractStatusMessage(EPaperAbstractStatus.Edit); makalah.DateModified = DateTime.UtcNow; string newFullPaperFileName = makalah.FullPaperFileName; if (includeNewFullPaper) { newFullPaperFileName = GetFileNameFromDateTime(DateTime.UtcNow, "Ppr", ".pdf"); string fullPaperPath = Server.MapPath(GlobalData.PaperFileSavePath); if (!Directory.Exists(fullPaperPath)) { Directory.CreateDirectory(fullPaperPath); } string fullPaperSaveFilePath = Path.Combine(fullPaperPath, newFullPaperFileName); model.FullPaperFileUpload.SaveAs(fullPaperSaveFilePath); //hapus file yang lama if (!string.IsNullOrEmpty(makalah.FullPaperFileName)) { string oldFileName = makalah.FullPaperFileName; FileInfo oldFullPaper = new FileInfo(Path.Combine(fullPaperPath, oldFileName)); if (oldFullPaper.Exists) { oldFullPaper.Delete(); } } makalah.FullPaperFileName = newFullPaperFileName; //Jika Full Paper baru ditambahkan if (makalah.FullPaperDateSubmitted == null) { makalah.FullPaperStatus = Enums.EFullPaperStatus.Baru; makalah.FullPaperConfirmMessage = GlobalData.GetFullPaperStatusMessage(EFullPaperStatus.Baru); makalah.FullPaperDateSubmitted = DateTime.UtcNow; } else { makalah.FullPaperStatus = Enums.EFullPaperStatus.Edit; makalah.FullPaperConfirmMessage = GlobalData.GetFullPaperStatusMessage(EFullPaperStatus.Edit); } } db.Entry(makalah).State = EntityState.Modified; db.SaveChanges(); string informationMessage = "Makalah telah diupdate."; return(RedirectToAction("Index", new { InformationMessage = informationMessage })); } catch (Exception ex) { ViewBag.ErrorMessage = ex.Message; return(View("Error")); } }