//hotels of the application crud methods public ActionResult AddHotel(int id) { Application s = systRepo.FindById(id); List <int> i = new List <int>(); hotels = hotelRepo.FindAll().ToList(); //deleting the hotels that the application already has //You can't delete a hotel while in the loop so first save the positions and after that loop it and delete the hotels foreach (Hotel h in hotels) { h.Applications.ToList().ForEach(t => { if (t.ApplicationId == id && DateTime.Compare(t.EndDate, DateTime.Today) > 0) { i.Add(hotels.IndexOf(h)); } }); } //after deleting a hotel the indexes change? Use 'j' to solve this for (int j = 0; j < i.Count(); j++) { hotels.RemoveAt(i.ElementAt(j) - j); } IEnumerable <AddHotel> list = hotels.Select(t => new AddHotel(t)); AddHotelToApplicationViewModel model = new AddHotelToApplicationViewModel(list.ToList()); return(View(model)); }
public ActionResult addHotelConfirmed(int id, AddHotelToApplicationViewModel model) { Application s = systRepo.FindById(id); if (s == null) { return(HttpNotFound()); } try { for (int i = 0; i < model.Hotels.Count(); i++) { if (model.Hotels[i].Checked == true) { Hotel h = hotelRepo.FindByCode(model.Hotels[i].HotelId); HotelApplication ha = new HotelApplication(); //fill up the object HA from the model etc MapToApplication(ha, model.Hotels[i], s, h); s.addHotel(ha); h.addApplication(ha); hotelRepo.SaveChanges(); TempData["message"] = String.Format("Hotel {0} was added to the list", h.Name); } } } catch (Exception ex) { TempData["error"] = "There was a problem when trying to add the hotel. Try again later. If this keeps happening, please contact the IT administrator"; //if the hotel was added but the error came from the DB //s.removeHotel(hotelRepo.FindByCode(hotelId).Applications.Where(t => t.ApplicationId == id).First()); return(AddHotel(id)); } return(RedirectToAction("Details", new { id = id })); }