// // GET: /Shop/ public ActionResult Index() { if (Session["Cart"] == null) // haven't been to db yet { try { ProductViewModel prod = new ProductViewModel(); List<ProductViewModel> Prods = prod.GetAll(); if (Prods.Count() > 0) { CartItem[] myCart = new CartItem[Prods.Count]; // array int ctr = 0; // build CartItem array from List contents foreach (ProductViewModel p in Prods) { CartItem item = new CartItem(); item.ProdCd = p.ProdCode; item.ProdName = p.ProdName; item.Graphic = p.Graphic; item.Msrp = p.Msrp; item.Description = p.Description; item.Qty = 0; myCart[ctr++] = item; } Session["Cart"] = myCart; // load to session } } catch (Exception ex) { ViewBag.Message = "Catalogue Problem - " + ex.Message; } } return View(); }
/// <summary> /// Add the order, package up dictionary and send it to model layer /// </summary> /// <param name="items"></param> /// <param name="user"></param> /// <param name="amt"></param> /// <param name="shipDate"></param> /// <returns></returns> public void AddOrder(CartItem[] items, int cid, double amt) { Dictionary<string, Object> dictionaryReturnValues = new Dictionary<string, Object>(); Dictionary<string, Object> dictionaryOrder = new Dictionary<string, Object>(); try { Message = ""; BackOrderFlag = 0; OrderID = -1; OrderModel myData = new OrderModel(); int idx = 0; string[] prodcds = new string[items.Length]; int[] qty = new int[items.Length]; Decimal[] sellPrice = new Decimal[items.Length]; foreach (CartItem item in items) { prodcds[idx] = item.ProdCd; sellPrice[idx] = item.Msrp; qty[idx++] = item.Qty; } dictionaryOrder["prodcd"] = prodcds; dictionaryOrder["qty"] = qty; dictionaryOrder["msrp"] = sellPrice; dictionaryOrder["cid"] = cid; dictionaryOrder["amt"] = amt; dictionaryReturnValues = (Dictionary<string, Object>)Deserializer(myData.AddOrder(Serializer(dictionaryOrder))); OrderID = Convert.ToInt32(dictionaryReturnValues["orderid"]); BackOrderFlag = Convert.ToInt32(dictionaryReturnValues["boflag"]); Message = Convert.ToString(dictionaryReturnValues["message"]); } catch (Exception ex) { ErrorRoutine(ex, "Order", "AddOrder"); Message = ex.Message; } }