public ActionResult EditVehicle(VehicleViewModel vm) { if (!ModelState.IsValid) { return(View("EditVehicle", vm)); } else { HorseplayContext db = new HorseplayContext(); vm.Rebuild(db); string plate = vm.Vehicle.Plate.Trim(); if (db.Vehicles.Any(v => v.Plate == plate && v.VehicleId != vm.Vehicle.VehicleId)) { ModelState.AddModelError(string.Empty, "Pojazd o takiej tablicy rejestracyjnej już istnieje. Numer rejestracyjny musi być unikalny"); return(View("EditVehicle", vm)); } else { int tenantId = (int)Session["TenantId"]; var vehs = db.Vehicles.Where(v => v.VehicleId == vm.Vehicle.VehicleId && v.TenantId == tenantId); if (vehs.Any()) { Vehicle oldVeh = vehs.FirstOrDefault(); oldVeh.dateModified = DateTime.Now; oldVeh.modifiedBy = (int)Session["UserId"]; oldVeh.Plate = plate; oldVeh.Brand = vm.Vehicle.Brand; oldVeh.Model = vm.Vehicle.Model; oldVeh.ProductionYear = vm.Vehicle.ProductionYear; oldVeh.Capacity = vm.Vehicle.Capacity; oldVeh.RegistrationDate = vm.Vehicle.RegistrationDate; oldVeh.ServiceDate = vm.Vehicle.ServiceDate; oldVeh.Type = vm.Vehicle.Type; oldVeh.Vin = vm.Vehicle.Vin; oldVeh.DefaultUser = vm.IntToEmployee(vm.SelectedUser); oldVeh.Description = vm.Vehicle.Description; db.Entry(oldVeh).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("GetVehicles")); } else { return(new HttpNotFoundResult("Nie oczekiwany błąd")); } } } }
public ActionResult CreateVehicle(VehicleViewModel vm) { if (!ModelState.IsValid) { return(View("CreateVehicle", vm)); } else { string plate = vm.Vehicle.Plate.Trim(); int tenantId = (int)Session["TenantId"]; HorseplayContext db = new HorseplayContext(); if (plate.Length > 0) { if (db.Vehicles.Any(v => v.Plate == plate && v.TenantId == tenantId)) { ModelState.AddModelError(string.Empty, "Pojazd o takiej tablicy rejestracyjnej już istnieje. Numer rejestracyjny musi być unikalny"); return(View("CreateVehicle", vm)); } } vm.Rebuild(db); //all is fine. Let's create this m**********r Vehicle Vehicle = new Vehicle(); Vehicle.Plate = plate; Vehicle.Brand = vm.Vehicle.Brand; Vehicle.Model = vm.Vehicle.Model; Vehicle.ProductionYear = vm.Vehicle.ProductionYear; Vehicle.Capacity = vm.Vehicle.Capacity; Vehicle.RegistrationDate = vm.Vehicle.RegistrationDate; Vehicle.ServiceDate = vm.Vehicle.ServiceDate; Vehicle.Type = vm.Vehicle.Type; Vehicle.Vin = vm.Vehicle.Vin; Vehicle.DefaultUser = vm.IntToEmployee(vm.SelectedUser); Vehicle.TenantId = tenantId; Vehicle.addedBy = (int)Session["UserId"]; Vehicle.dateAdded = DateTime.Now; db.Vehicles.Add(Vehicle); db.SaveChanges(); return(RedirectToAction("GetVehicles")); } }