Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. [HttpPost]
  2. public ActionResult SelectProduct(BrandViewModel vm)
  3. {
  4. Dictionary<int, object> cart;
  5.  
  6. if (HttpContext.Session.Get<Dictionary<int, object>>("cart") == null)
  7. {
  8. cart = new Dictionary<int, object>();
  9. }
  10. else
  11. {
  12. cart = HttpContext.Session.Get<Dictionary<int, object>>("cart");
  13. }
  14.  
  15. ProductViewModel[] menu = HttpContext.Session.Get<ProductViewModel[]>("menu");
  16.  
  17. String retMsg = "";
  18.  
  19. foreach (ProductViewModel item in menu)
  20. {
  21. if (Convert.ToInt32(item.Id) == vm.Id)
  22. {
  23. if (vm.Qty > 0) // update only selected item
  24. {
  25. item.Qty = vm.Qty;
  26. retMsg = vm.Qty + " - item(s) Added!";
  27. cart[Convert.ToInt32(item.Id)] = item;
  28. }
  29. else
  30. {
  31. item.Qty = 0;
  32. cart.Remove(Convert.ToInt32(item.Id));
  33. retMsg = "item(s) Removed!";
  34. }
  35. vm.BrandId = item.BrandId;
  36. break;
  37. }
  38. }
  39.  
  40. ViewBag.AddMessage = retMsg;
  41. HttpContext.Session.Set<Dictionary<int, object>>("cart", cart);
  42. vm.SetBrands(HttpContext.Session.Get<List<Brand>>("brands"));
  43. return View("Index", vm);
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement