// // GET: /Cart/ public ActionResult AddProduct() { CartProduct product = new CartProduct(); int tmp = 0; int.TryParse(Request.Form["id"], out tmp); product.ID = tmp; tmp = 0; int.TryParse(Request.Form["Quantity"], out tmp); product.Quantity = tmp; IntoSport.Helpers.ProductHelper productdetails = new IntoSport.Helpers.ProductHelper(); List <string> keys = new List <string>(); foreach (KeyValuePair <string, string> details in productdetails.getDetails(product.ID)) { if (!keys.Contains(details.Key)) { keys.Add(details.Key); } } foreach (string key in keys) { if (Request.Form[key] != null) { DetailWaarde detailwaarde = new DetailWaarde(); detailwaarde.waarde = Request.Form.Get(key).Split(',')[1]; product.DetailWaardeList.Add(detailwaarde); } } Cart cart = Session["cart"] as Cart; if (cart != null) { cart.productList.Add(product); Session["cart"] = cart; } else { Cart newCart = new Cart(); newCart.productList.Add(product); Session["cart"] = newCart; } return(RedirectToAction("Index")); }
public ActionResult Product(Product product, FormCollection collection, HttpPostedFileBase thumbnail, HttpPostedFileBase afbeelding) { Product p = new Product(); if (ModelState.IsValid) { if ((thumbnail != null && thumbnail.ContentLength > 0) && (afbeelding != null && afbeelding.ContentLength > 0)) { //Pak de naam van het bestand var thumbName = Path.GetFileName(thumbnail.FileName); var imgName = Path.GetFileName(afbeelding.FileName); // Afbeeldingen opslaan in de bijbehorende folders var thumbPath = Path.Combine(Server.MapPath("~/Template/images/products/thumbnail"), "" + Models.Product.GetLastProductID() + ".png"); var imgPath = Path.Combine(Server.MapPath("~/Template/images/products"), "" + Models.Product.GetLastProductID() + ".png"); thumbnail.SaveAs(thumbPath); afbeelding.SaveAs(imgPath); p.afbeelding = "Template/images/products/" + Models.Product.GetLastProductID() + ".png"; p.thumbnail = "Template/images/products/thumbnail/" + Models.Product.GetLastProductID() + ".png"; } string[] categories = new string[0]; if (collection.AllKeys.Contains("subcat")) { categories = collection["subcat"].Split(','); } p.id = int.Parse(collection["id"]); p.naam = collection["naam"]; p.beschrijving = collection["beschrijving"]; p.prijs = double.Parse(collection["prijs"]); p.korting = (collection["korting"] != null ? int.Parse(collection["korting"]) : 0); p.voorraad = (collection["voorraad"] != null ? int.Parse(collection["voorraad"]) : 0); if (thumbnail == null) { p.thumbnail = ""; } if (afbeelding == null) { p.afbeelding = ""; } if (p.id != 0) { p.Update(); p.UpdateCategorie(categories); } else { p.id = p.Insert(); p.InsertCategorie(categories); } JObject jsonobject = JObject.Parse(collection["detailsJSON"]); foreach (var d in jsonobject) { Detail detail = new Detail(); detail.naam = d.Key; foreach (string waarde in d.Value.ToString().Split(',')) { DetailWaarde detailwaarde = new DetailWaarde(); detailwaarde.waarde = Regex.Replace(waarde, @"[\[\]""]+", ""); detail.waardes.Add(detailwaarde); } detail.Save(p); } ViewData.Add("msg", "De wijzigingen zijn succesvol opgeslagen."); Product p2 = new Product(p.id); ViewData.Add("product", p2); ViewData.Add("getCategories", Models.Category.getAllCategories()); ViewData.Add("details", Models.Detail.getAllDetails()); return(View("Product")); } ViewData.Add("product", p); ViewData.Add("details", Models.Detail.getAllDetails()); ViewData.Add("getCategories", Models.Category.getAllCategories()); return(View()); }