public static ReservationViewModel CreateReservationViewModel(Reservation reservation) { ApplicationDbContext db = new ApplicationDbContext(); ReservationViewModel rvm = new ReservationViewModel(); rvm.Branch = reservation.Branch; rvm.CarCode = null; return rvm; }
public ActionResult Index(string FilterCarCode, string FilterCity, string CityId, string StartDate, string EndDate) { ReservationViewModel rvm = new ReservationViewModel(); try { rvm.StartDate = DateTime.Parse(StartDate); rvm.EndDate = DateTime.Parse(EndDate); } catch (Exception)//Defensive programming { rvm.StartDate = Common.Tomorrow(); rvm.EndDate = Common.DayAfterTomorrow(); } //rvm.CarData = db.Cars.ToList(); //var reservations = db.Reservations.Include(r => r.Branch).Include(r => r.Car); var cars = db.Cars.Include(c => c.Branch).Include(c => c.CarType).Include(c => c.Reservations); cars = cars.Where(c => c.CarStatus == CarStatus.Available);//Todo: Check reservation dates if (!String.IsNullOrWhiteSpace(FilterCarCode)) { cars = cars.Where(s => s.CarType.CarCode == FilterCarCode); } //ViewBag.CarCodeFilter = FilterCarCode; if (!String.IsNullOrWhiteSpace(CityId)) { int cityId; if (int.TryParse(CityId, out cityId)) { cars = cars.Where(s => s.Branch.City.CityId==cityId); } } ViewBag.FilterCity = FilterCity; ViewBag.CityId = new SelectList(db.Cities, "CityId", "CityName"); var distinctCarCodes = (from c in db.CarTypes select new { CarCode = c.CarCode }).Distinct(); ViewBag.CarCodeId = new SelectList(distinctCarCodes, "CarCode", "CarCode"); rvm.CarData = cars.ToList(); return View(rvm); }