public async Task <IActionResult> Edit(int id, [Bind("FileName,Description,Category,UploadDateTime,File")] PDFDocumentEditViewModel viewModel) { if (!ModelState.IsValid) { return(View(viewModel)); } byte[] newAttachment = null; if (viewModel.File != null) //new file attached { PDFDocumentValidator.ValidatePDFAttachment(viewModel.File, ModelState); if (!ModelState.IsValid) { return(View(viewModel)); } newAttachment = FileHelpers.ReadBytes(viewModel.File); } var sourceDocument = GetDocument(id); if (sourceDocument == null) { return(NotFound()); } //updates sourceDocument with values from viewModel, replaces Data if newAttachment is not null sourceDocument = PDFDocumentViewModelToModelHelper.ViewModelToModel(viewModel, newAttachment, sourceDocument); try { _context.Update(sourceDocument); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PDFDocumentExists(viewModel.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Create([Bind("FileName,Description,Category,File")] PDFDocumentCreateViewModel viewModel) { if (!ModelState.IsValid) { return(View(viewModel)); } PDFDocumentValidator.ValidatePDFAttachment(viewModel.File, ModelState); if (!ModelState.IsValid) { return(View(viewModel)); } var fileData = FileHelpers.ReadBytes(viewModel.File); var PDFDocument = PDFDocumentViewModelToModelHelper.ViewModelToModel(viewModel, fileData); _context.Add(PDFDocument); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }