public ActionResult Edit(EditViewModel editViewModel) { if (editViewModel == null) { throw new HttpException(400, "Bad Request"); } var document = DocumentService.FindById(editViewModel.DocumentId); if (document == null) { throw new HttpException(404, "Not Found"); } document.Name = editViewModel.Name; document.Description = editViewModel.Description; document.DocumentCategory = DocumentCategoryService.FindById(editViewModel.DropDownList_DocumentCategory_Property.SelectedDocumentCategory); document.DocumentCategoryId = editViewModel.DropDownList_DocumentCategory_Property.SelectedDocumentCategory != -1 ? (int?)editViewModel.DropDownList_DocumentCategory_Property.SelectedDocumentCategory : null; var oldUploadFileUrl = document.UploadFileUrl; foreach (string file in Request.Files) { if (Request.Files[file].ContentLength <= 0 || Request.Files[file] == null) continue; string pathToSave = Server.MapPath("~/Files/Documents"); string oldFilename = Path.GetFileName(Request.Files[file].FileName); string fileExtension = Path.GetExtension(Request.Files[file].FileName); string uploadFileName = String.Format(Guid.NewGuid() + fileExtension); Request.Files[file].SaveAs(Path.Combine(pathToSave, uploadFileName)); document.OldFileName = Request.Files[file].FileName; document.UploadFileUrl = Path.Combine(pathToSave, uploadFileName); } if (oldUploadFileUrl != document.UploadFileUrl) { if (System.IO.File.Exists(oldUploadFileUrl)) { System.IO.File.Delete(oldUploadFileUrl); } } DocumentService.UpdateDocument(document); TempData["DocumentId"] = document.DocumentId; TempData["DocumentName"] = document.Name; TempData["Message"] = DocumentsMessage.EditSuccess; return RedirectToAction("index", "documents"); }
public ActionResult Edit(int? id) { if (id == null) { throw new HttpException(400, "Bad Request"); } var document = DocumentService.FindById(id.Value); if (document == null) { throw new HttpException(404, "Not Found"); } var dropdownlist_DocumentCategory_Property = new EditViewModel.DropDownList_DocumentCategory() { DocumentCategories = DocumentCategoryService.GetDocumentCategories().ToList(), SelectedDocumentCategory = document.DocumentCategory != null ? document.DocumentCategory.DocumentCategoryId : -1 }; var editViewModel = new EditViewModel() { DocumentId = document.DocumentId, Name = document.Name, Description = document.Description, DropDownList_DocumentCategory_Property = dropdownlist_DocumentCategory_Property, OldUploadFileUrl = document.UploadFileUrl, OldFileName = document.OldFileName, }; return View(editViewModel); }