public ActionResult AddProductToCart(ProductPartOrderViewModel partOrder) { // Get the login from session and check if a person is set, is not return a site with a javascript var login = (Login)Session["Login"]; if (login.PersonId == 0) { return(Content("<script language='javascript' type='text/javascript'>alert('Du skal være logget ind får at tilføje til kurv :)');</script>")); } // Get the cart from Session var cart = (Cart)Session["ShoppingCart"]; var proxy = new BestilNemtServiceClient(); // Get the product by a partOrder product id. var product = proxy.GetProduct(partOrder.Product.Id); // Make the PartOrder object var po = new PartOrder { Product = product, Amount = partOrder.Amount, PartPrice = product.Price * partOrder.Amount, Cart = cart }; // Get all PartOrders from cart var partOrders = cart.PartOrders; // There is a partOrder // isFound is used to check if a partOrder is allready added var isFound = false; // Loop for every partOrdeers foreach (var partOrderLoop in partOrders) { // If the product ids not match if (partOrderLoop.Product.Id != partOrder.Product.Id) { continue; } // Update the amount partOrderLoop.Amount = partOrder.Amount + partOrderLoop.Amount; // The PartOrder is found isFound = true; } // If the partOrder is not allready added, add it if (!isFound) { // Add the partOrder to the list partOrders.Add(po); } // Save the list of PartOrders to the cart cart.PartOrders = partOrders; // Update the session of Cart Session["ShoppingCart"] = cart; // Get the shop from session var shop = (Shop)Session["Shop"]; // Return the view to Product with the Shop id return(RedirectToAction("Product", new { id = shop.Id })); }
/// <summary> /// Return a view of a specific product with a product id /// The id is a Product id /// </summary> /// <param name="id"></param> /// <returns></returns> public ActionResult ProductPage(int?id) { // If no id is set redirect to frontpage if (id == null) { return(RedirectToAction("Index", "Home")); } var proxy = new BestilNemtServiceClient(); // Get the PartOrder with a product by a product id var pvm = new ProductPartOrderViewModel { Product = proxy.GetProduct(id.Value) }; // Return the view return(View(pvm)); }