public async Task <IActionResult> Edit(int id, [Bind("Id,Name,Price,Description,AttachmentUpload")] EditGoodViewModel viewModel) { var model = _context.Goods.Find(id); if (id != viewModel.Id) { return(NotFound()); } if (ModelState.IsValid) { try { _mapper.Map(viewModel, model); _context.Update(model); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!GoodModelExists(viewModel.Id)) { return(NotFound()); } else { throw; } } if (viewModel.AttachmentUpload != null) { string extension = Path.GetExtension(viewModel.AttachmentUpload.FileName); string path = "/Files/" + viewModel.Id + extension; string absPath = _appEnvironment.WebRootPath + model.Attachment; if (path != model.Attachment && System.IO.File.Exists(absPath)) { System.IO.File.Delete(absPath); } using (var fileStream = new FileStream(_appEnvironment.WebRootPath + path, FileMode.Create)) { await viewModel.AttachmentUpload.CopyToAsync(fileStream); } model.Attachment = path; _context.SaveChanges(); } } else { var partialViewHtml = await this.RenderViewAsync(nameof(Edit), viewModel, true); TempData.Put(Constants.ERROR_MODAL, partialViewHtml); } return(RedirectToAction(nameof(Index))); }
public EditGood(Good good) { InitializeComponent(); model = new EditGoodViewModel(good, new HttpClient(), this); BindingContext = model; }