public ActionResult AddToCart(ViewProductViewModel viewModel) { // Validate if (viewModel.ProductNumber == 0 || viewModel.SupplierNumber == 0) { TempData["errorMessage"] = "Error: We could not add the item to the cart."; return(JavaScript("window.location = window.location.href;")); } if (ModelState.IsValid) { string userId = User.Identity.GetUserId(); bool anonymousUser = (userId == null); ShoppingCart cart = GetCartFromSession(userId); if (cart.Order.OrderStatus == "Locked") { TempData["errorMessage"] = "Your cart is locked because you are in the process of checking out. Open your cart to complete or cancel the checkout process."; if (Request.IsAjaxRequest()) { return(JavaScript("window.location = window.location.href;")); } else { return(RedirectToAction("Cart", "ShoppingCart")); } } // CheckQuantity if (viewModel.CustodianQuantityOnHand < viewModel.Quantity) { viewModel.SetInstances(viewModel.ProductNumber, viewModel.SupplierNumber); if (Request.IsAjaxRequest()) { return(PartialView("_ViewProductModal", viewModel)); } else { viewModel.Reviews = ProductReviewsCollection.GetReviewsOnly(viewModel.ProductNumber, viewModel.SupplierNumber, 0); return(View("ViewProduct", viewModel)); } } FreeMarketObject result; result = cart.AddItemFromProduct(viewModel.ProductNumber, viewModel.SupplierNumber, viewModel.Quantity, viewModel.CustodianNumber, viewModel.SelectedPackageType); if (result.Result == FreeMarketResult.Success) { // New item added if (!string.IsNullOrEmpty(result.Message)) { TempData["message"] = result.Message; } else if (!string.IsNullOrEmpty(result.Message)) { TempData["errorMessage"] = result.Message; } } if (Request.IsAjaxRequest()) { return(JavaScript("window.location = window.location.href;")); } else { viewModel.Reviews = ProductReviewsCollection.GetReviewsOnly(viewModel.ProductNumber, viewModel.SupplierNumber, 0); return(RedirectToAction("Cart", "ShoppingCart")); } } // Validation Error else { // Prepare viewModel.SetInstances(viewModel.ProductNumber, viewModel.SupplierNumber); if (Request.IsAjaxRequest()) { return(PartialView("_ViewProductModal", viewModel)); } else { viewModel.Reviews = ProductReviewsCollection.GetReviewsOnly(viewModel.ProductNumber, viewModel.SupplierNumber, 0); return(View("ViewProduct", viewModel)); } } }