public ActionResult Edit(EditViewModel editViewModel) { if (editViewModel == null) { throw new HttpException(400, "Bad Request"); } var contract = ContractService.FindById(editViewModel.ContractId); if (contract == null) { throw new HttpException(404, "Not Found"); } contract.Name = editViewModel.Name; contract.Description = editViewModel.Description; var selectedContractStatus = ContractStatusService.FindById(editViewModel.DropDownList_ContractStatus_Property.SelectedContractStatus); contract.ContractStatus = selectedContractStatus; contract.ContractStatusId = editViewModel.DropDownList_ContractStatus_Property.SelectedContractStatus != -1 ? (int?)editViewModel.DropDownList_ContractStatus_Property.SelectedContractStatus : null; contract.ExpiredOn = !String.IsNullOrEmpty(editViewModel.ExpiredOn) ? (DateTime?)DateTime.ParseExact(editViewModel.ExpiredOn, "dd/MM/yyyy", CultureInfo.InvariantCulture) : null; var oldContractFile = contract.ContractFile; foreach (string file in Request.Files) { if (Request.Files[file].ContentLength <= 0 || Request.Files[file] == null) continue; string pathToSave = Server.MapPath("~/Files/Contracts"); 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)); contract.OldContractFileName = Request.Files[file].FileName; contract.ContractFile = Path.Combine(pathToSave, uploadFileName); } if (oldContractFile != contract.ContractFile) { if (System.IO.File.Exists(oldContractFile)) { System.IO.File.Delete(oldContractFile); } } ContractService.UpdateContract(contract); TempData["ContractId"] = contract.ContractId; TempData["ContractName"] = contract.Name; TempData["Message"] = ContractsMessage.EditSuccess; return RedirectToAction("index", "contracts"); }
public ActionResult Edit(int? id) { if (id == null) { throw new HttpException(400, "Bad Request"); } var contract = ContractService.FindById(id.Value); if (contract == null) { throw new HttpException(404, "Not Found"); } var dropdownlist_ContractStatus_Property = new EditViewModel.DropDownList_ContractStatus() { ContractStatuses = ContractStatusService.GetContractStatuses().Where(cs => cs.Name == "Unsent" || cs.Name == "Sent" || cs.Name == "Received").ToList(), SelectedContractStatus = contract.ContractStatus.ContractStatusId }; var editViewModel = new EditViewModel() { ContractId = contract.ContractId, Name = contract.Name, Description = contract.Description, ContractFile = contract.ContractFile, OldContractFileName = contract.OldContractFileName, DropDownList_ContractStatus_Property = dropdownlist_ContractStatus_Property, ExpiredOn = contract.ExpiredOn != null ? contract.ExpiredOn.Value.ToString("dd/MM/yyyy") : "", }; return View(editViewModel); }