// applications of the hotel crud methods public ActionResult AddApplication(string id) { Hotel h = hotelRepo.FindByCode(id); List <int> i = new List <int>(); List <Application> systems = sysRepo.FindAll().ToList(); //deleting the systems that the application already has //You can't delete a system while in the loop so first save the positions and after that loop it and delete the hotels foreach (Application s in systems) { s.Hotels.ToList().ForEach(t => { if (t.HotelId == id && DateTime.Compare(t.EndDate, DateTime.Today) > 0) { i.Add(systems.IndexOf(s)); } }); } //after deleting a system the indexes change? Use 'j' to solve this for (int j = 0; j < i.Count(); j++) { systems.RemoveAt(i.ElementAt(j) - j); } IEnumerable <AddApplication> list = systems.Select(t => new AddApplication(t)); AddApplicationToHotelViewModel model = new AddApplicationToHotelViewModel(list.ToList()); return(View(model)); }
public ActionResult ConfirmAddApplication(string id, AddApplicationToHotelViewModel model) { Hotel h = hotelRepo.FindByCode(id); if (h == null) { return(HttpNotFound()); } try { for (int i = 0; i < model.Applications.Count(); i++) { if (model.Applications[i].Checked == true) { Application app = sysRepo.FindById(model.Applications[i].ApplicationId); HotelApplication ha = new HotelApplication(); //fill up the HA from the model etc MapToHotelApp(ha, model.Applications[i], h, app); app.addHotel(ha); h.addApplication(ha); hotelRepo.SaveChanges(); TempData["message"] = String.Format("Application {0} was succesfully added to the hotel", app.Name); } } } catch (Exception ex) { TempData["error"] = "There was a problem when adding te application to the hotel. Please try again later"; //if the app was added but the error came from de DB //h.removeApplication(sysRepo.FindById(ApplicationId).Hotels.Where(t => t.HotelId == id).First()); } return(RedirectToAction("Details", new { hotelId = id })); }