public ActionResult ReserveCar(int ID) { context data = new context(); ClientReserveCarViewModel viewModel = new ClientReserveCarViewModel(); viewModel.CarID = ID; viewModel.CarName = data.Cars.FirstOrDefault(i => i.ID == ID).Title; TempData["ID"] = ID; // not working return(View()); }
public ActionResult ReserveCar(int id, ClientReserveCarViewModel ViewModel) { ViewModel.CarID = id; context data = new context(); data.Cars.FirstOrDefault(ca => ca.ID == ViewModel.CarID).RentDate = DateTime.Today; data.Cars.FirstOrDefault(ca => ca.ID == ViewModel.CarID).period = ViewModel.Period; data.Cars.FirstOrDefault(ca => ca.ID == ViewModel.CarID).CarIsReserved(ViewModel.NationalId, ViewModel.Period); if (data.Clients.FirstOrDefault(cli => cli.ssn == ViewModel.NationalId) != null) { data.Clients.FirstOrDefault(cli => cli.NationalId == ViewModel.NationalId).RentedCarsHistory.Add(data.Cars.FirstOrDefault(ca => ca.ID == ViewModel.CarID)); } else { Client client = new Client { ssn = ViewModel.NationalId, NationalId = ViewModel.NationalId, City = ViewModel.City, Country = ViewModel.Country, Street = ViewModel.Street, FirstName = ViewModel.FirstName, LastName = ViewModel.LastName, Phone = ViewModel.Phone }; if (client.RentedCarsHistory == null) { client.RentedCarsHistory = new List <Car>(); } client.RentedCarsHistory.Add(data.Cars.FirstOrDefault(ca => ca.ID == ViewModel.CarID)); data.Clients.Add(client); } data.SaveChanges(); if (ModelState.IsValid) { return(RedirectToAction("ShowCarsCards")); } else { return(RedirectToAction("AddCar")); } }