public ActionResult Edit(int id) { Aircraft aircraft = _dataService.GetAircraftById(id); AircraftEditViewModel viewModel = new AircraftEditViewModel(aircraft); return(View(viewModel)); }
public ActionResult Edit(AircraftEditViewModel viewModel) { if (ModelState.IsValid) { Aircraft aircraft = _dataService.GetAircraftById(viewModel.Id); if (aircraft.RegistrationNumber != viewModel.RegistrationNumber && _dataService.GetAircraftByRegNumber(viewModel.RegistrationNumber) != null) { ModelState.AddModelError("RegistrationNumber", "Aircraft with registration number already exists in the database"); } else { aircraft = CopyDataFromViewModel(aircraft, viewModel); _dataService.UpdateAircraft(aircraft); return(RedirectToAction("Details", new { id = viewModel.Id })); } } return(View(viewModel)); }
public ActionResult Create(AircraftEditViewModel viewModel) { if (ModelState.IsValid) { if (_dataService.GetAircraftByRegNumber(viewModel.RegistrationNumber) == null) { Aircraft entity = new Aircraft(); entity = CopyDataFromViewModel(entity, viewModel); _dataService.AddAircraft(entity); return(RedirectToAction("Index")); } else { ModelState.AddModelError("RegistrationNumber", "Aircraft with registration number already exists in the database"); } } return(View(viewModel)); }
private Aircraft CopyDataFromViewModel(Aircraft entity, AircraftEditViewModel viewModel) { entity.Year = viewModel.Year; entity.BasedAt = viewModel.BasedAt; entity.Make = viewModel.Make; entity.CheckoutRequirements = viewModel.CheckoutRequirements; entity.CruiseSpeed = viewModel.CruiseSpeed; entity.Description = viewModel.Description; entity.EquipmentList = viewModel.EquipmentList; entity.FuelCapacity = viewModel.FuelCapacity; entity.HourlyRate = viewModel.HourlyRate; entity.MaxGrossWeight = viewModel.MaxGrossWeight; entity.MaxRange = viewModel.MaxRange; entity.Model = viewModel.Model; entity.Name = viewModel.Name; entity.OperationalNotes = viewModel.OperationalNotes; entity.RegistrationNumber = viewModel.RegistrationNumber; entity.Status = viewModel.Status; entity.StatusNotes = viewModel.StatusNotes; entity.TypeDesignation = viewModel.TypeDesignation; entity.UsefulLoad = viewModel.UsefulLoad; return(entity); }