public ActionResult Reserve(ProductDetailsViewModel moviemodel, DateTime?todaydate) { var product = db.Products.Find(moviemodel.ProductID); moviemodel.Product = product; if (todaydate.HasValue) { moviemodel.Month = todaydate.Value; moviemodel.Calendar = product.GetCalendar(todaydate.Value.Year, todaydate.Value.Month); return(View(moviemodel)); } if (ModelState.IsValid) { bool isvalid = true; //var user = db.Customers.Where(x => x.UserID == myUser).Select(c => c.Name); var useridTest = User.Identity.GetUserId(); var user = context.Users.FirstOrDefault(u => u.Id == useridTest); String em = user.Email; EmailUtility.registerProduct(product.Name + " has been booked from " + moviemodel.StartDate + " until " + moviemodel.EndDate + " by " + em); //start date and end date validtions, ifs, startdate.hasvalue if null will be false, datetime.today=today's date //startdate must be at least today, if missing, if not today seperate errorr //enddate check if null, along with or after startdate, must be later than start date for (DateTime d = moviemodel.StartDate ?? moviemodel.EndDate ?? DateTime.Today; d <= (moviemodel.EndDate ?? moviemodel.StartDate ?? DateTime.Today); d = d.AddDays(1)) { if (product.OpnInvDay(d) < moviemodel.Quantity) { //error, not enough available ModelState.AddModelError("Quantity", "Sorry, we're out of stock on those days"); isvalid = false; break; } if (DateTime.Now > moviemodel.StartDate) { ModelState.AddModelError("StartDate", "Select a present or future start date"); isvalid = false; break; } } if (moviemodel.StartDate < DateTime.Now /*|| moviemodel.EndDate > DateTime.Now*/) { ModelState.AddModelError("EndDate", "End date must be after start date"); isvalid = false; } if (!isvalid) { moviemodel.Calendar = product.GetCalendar(moviemodel.Month.Year, moviemodel.Month.Month); return(View(moviemodel)); } var userid = User.Identity.GetUserId(); var cart = db.Carts.FirstOrDefault(x => x.UserID == userid && x.OrderDate == null); if (cart == null) { cart = new Cart(); cart.UserID = userid; } var OLI = new OrderLineItem(); OLI.Cart = cart; OLI.EndDate = moviemodel.EndDate.Value; OLI.StartDate = moviemodel.StartDate.Value; OLI.ProductID = moviemodel.ProductID; OLI.Quantity = moviemodel.Quantity; db.OrderLineItems.Add(OLI); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(moviemodel)); }