public ActionResult Index(CompanyBikeViewModel model) { if (ModelState.IsValid) { var notBookedCompanyBikes = new List <CompanyBike>(); var bookings = db.Bookings.Where(c => c.EndDate > DateTime.Today).ToList(); foreach (var companyBike in model.CompanyBikes) { var isNotBooked = true; foreach (var booking in bookings) { if (companyBike.Id == booking.CompanyBikeId) { if (model.EndDate >= booking.StartDate && model.StartDate <= booking.EndDate) { isNotBooked = false; } } } if (isNotBooked) { notBookedCompanyBikes.Add(companyBike); } } model.CompanyBikes = notBookedCompanyBikes; model.IsValid = true; } return(View(model)); }
public ActionResult Index(int?id) { var companyBikes = db.CompanyBikes.Include(c => c.Bike).Include(c => c.CompanyLocation); var model = new CompanyBikeViewModel(); var bookings = db.Bookings.ToList(); // set up feedback values foreach (var companyBike in companyBikes.ToList()) { int count = 0; int sum = 0; foreach (var b in bookings) { if (b.CompanyBikeId == companyBike.Id) { if (b.Feedback != null) { count += 1; sum += b.Feedback ?? 0; } } } if (count != 0 && sum != 0) { companyBike.Rating = Math.Round(sum / (double)count, 1); } } if (id != null) { companyBikes = companyBikes.Where(c => c.CompanyLocationId == id); if (companyBikes.Count() == 0) { return(HttpNotFound()); } model.CompanyBikes = companyBikes.ToList(); return(View(model)); } model.CompanyBikes = companyBikes.ToList(); return(View(model)); }