public ActionResult Edit(EditViewModel editViewModel) { if (editViewModel == null) { throw new HttpException(400, "Bad Request"); } var voucherTemplate = VoucherTemplateService.FindById(editViewModel.VoucherTemplateId); if (voucherTemplate == null) { throw new HttpException(404, "Not Found"); } voucherTemplate.Name = editViewModel.Name; voucherTemplate.Description = editViewModel.Description; var oldTemplateFile = voucherTemplate.TemplateFile; foreach (string file in Request.Files) { if (Request.Files[file].ContentLength <= 0 || Request.Files[file] == null) continue; string pathToSave = Server.MapPath("~/Files/VoucherTemplates"); string fileExtension = Path.GetExtension(Request.Files[file].FileName); string uploadFileName = String.Format(Guid.NewGuid() + fileExtension); Request.Files[file].SaveAs(Path.Combine(pathToSave, uploadFileName)); voucherTemplate.TemplateFile = Path.Combine(pathToSave, uploadFileName); } if (oldTemplateFile != voucherTemplate.TemplateFile) { if (System.IO.File.Exists(oldTemplateFile)) { System.IO.File.Delete(oldTemplateFile); } } VoucherTemplateService.UpdateVoucherTemplate(voucherTemplate); TempData["VoucherTemplateId"] = voucherTemplate.VoucherTemplateId; TempData["VoucherTemplateName"] = voucherTemplate.Name; TempData["Message"] = DocumentsMessage.EditSuccess; return RedirectToAction("index", "vouchertemplates"); }
public ActionResult Edit(int? id) { if (id == null) { throw new HttpException(400, "Bad Request"); } var voucherTemplate = VoucherTemplateService.FindById(id.Value); if (voucherTemplate == null) { throw new HttpException(404, "Not Found"); } var editViewModel = new EditViewModel() { VoucherTemplateId = voucherTemplate.VoucherTemplateId, Name = voucherTemplate.Name, Description = voucherTemplate.Description, TemplateFile = voucherTemplate.TemplateFile, }; return View(editViewModel); }