public ActionResult Create(VehicleViewModel vehicle) { try { if (ModelState.IsValid) { Vehicle vehicleobj = dbContex.Vehicles.Find(vehicle.Id); if (vehicleobj != null) { ModelState.AddModelError("", "The Vehicle was created."); } else { Vehicle newVehicle = vehicle.ToVehicle(vehicleobj); dbContex.Vehicles.Add(newVehicle); dbContex.SaveChanges(); return(RedirectToAction("Index")); } } } catch (RetryLimitExceededException) { ModelState.AddModelError("", "Unable to save changes. Try again, Please se Administrator."); throw; } return(View(vehicle)); }
public ActionResult Edit(VehicleViewModel vehicle) { var vehileToUpdate = dbContex.Vehicles.Find(vehicle.Id); Vehicle vehileNew = vehicle.ToVehicle(vehileToUpdate); if (vehileToUpdate == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (ModelState.IsValid) { try { dbContex.Vehicles.Attach(vehileNew); dbContex.Entry(vehileNew).State = EntityState.Modified; dbContex.SaveChanges(); return(RedirectToAction("Index")); } catch (RetryLimitExceededException) { ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator."); } } VehicleViewModel model = new VehicleViewModel(vehileNew); return(View(model)); }
public IActionResult Edit(int id, VehicleViewModel vvm) { if (ModelState.IsValid) { var v = vvm.ToVehicle(); svc.UpdateVehicle(id, v); // RedirectToAction("Details", new {id = v.Id}); Alert("Vehicle Updated", AlertType.success); return(RedirectToAction(nameof(Index))); } return(View(vvm)); }
public IActionResult Create(VehicleViewModel vvm) { if (ModelState.IsValid) { var v = vvm.ToVehicle(); var ok = svc.AddVehicle(v); if (ok == null) { Alert("Vehicle Registration Already Used", AlertType.warning); } else { Alert("Vehicle Created!", AlertType.success); } return(RedirectToAction(nameof(Index))); } return(View(vvm)); }