コード例 #1
0
        public IActionResult Index()
        {
            CatalogueViewModel vm = new CatalogueViewModel();

            // only build the catalogue once
            if (HttpContext.Session.GetObject <List <Brand> >(SessionVars.Brands) == null)
            {
                try
                {
                    BrandModel brandModel = new BrandModel(_db);
                    // now load the Brands
                    List <Brand> Brands = brandModel.GetAll();
                    HttpContext.Session.SetObject(SessionVars.Brands, Brands);
                    vm.SetBrands(HttpContext.Session.GetObject <List <Brand> >(SessionVars.Brands));
                }
                catch (Exception ex)
                {
                    ViewBag.Message = "Catalogue Problem - " + ex.Message;
                }
            }
            else
            {
                vm.SetBrands(HttpContext.Session.GetObject <List <Brand> >(SessionVars.Brands));
            }

            GetAllProducts();

            return(View(vm));
        }
コード例 #2
0
        public ActionResult SelectItem(CatalogueViewModel vm)
        {
            Dictionary <string, object> ShoppingCart;

            if (HttpContext.Session.GetObject <Dictionary <string, Object> >(SessionVars.ShoppingCart) == null)
            {
                ShoppingCart = new Dictionary <string, object>();
            }
            else
            {
                ShoppingCart = HttpContext.Session.GetObject <Dictionary <string, object> >(SessionVars.ShoppingCart);
            }
            ProductViewModel[] catalogue = HttpContext.Session.GetObject <ProductViewModel[]>(SessionVars.Catalogue);
            String             retMsg    = "";

            foreach (ProductViewModel item in catalogue)
            {
                if (item.Id == vm.Id)
                {
                    if (vm.Qty > 0) // update only selected item
                    {
                        item.Qty = vm.Qty;
                        retMsg   = vm.Qty + " - item(s) Added!";
                        ShoppingCart[item.Id] = item;
                    }
                    else
                    {
                        item.Qty = 0;
                        ShoppingCart.Remove(item.Id);
                        retMsg = "item(s) Removed!";
                    }
                    vm.BrandId = item.BrandId;
                    break;
                }
            }
            ViewBag.AddMessage = retMsg;
            HttpContext.Session.SetObject(SessionVars.ShoppingCart, ShoppingCart);
            vm.SetBrands(HttpContext.Session.GetObject <List <Brand> >(SessionVars.Brands));
            return(View("Index", vm));
        }
コード例 #3
0
        public IActionResult SelectBrand(CatalogueViewModel vm)
        {
            BrandModel              brandModel   = new BrandModel(_db);
            ProductModel            productModel = new ProductModel(_db);
            List <Product>          items        = productModel.GetAllByBrand(vm.BrandId);
            List <ProductViewModel> vms          = new List <ProductViewModel>();

            if (items.Count > 0)
            {
                foreach (Product item in items)
                {
                    ProductViewModel mvm = new ProductViewModel();
                    mvm.Qty         = 0;
                    mvm.BrandId     = item.BrandId;
                    mvm.BrandName   = brandModel.GetName(item.BrandId);
                    mvm.ProductName = item.ProductName;

                    mvm.Description    = item.Description;
                    mvm.GraphicName    = item.GraphicName;
                    mvm.CostPrice      = item.CostPrice;
                    mvm.MSRP           = item.MSRP;
                    mvm.QtyOnHand      = item.QtyOnHand;
                    mvm.QtyOnBackOrder = item.QtyOnBackOrder;

                    mvm.Id             = item.Id;
                    mvm.HP             = item.HP;
                    mvm.ATTACK         = item.Attack;
                    mvm.DEFENCE        = item.Defence;
                    mvm.SPECIALATTACK  = item.SpecialAttack;
                    mvm.SPECIALDEFENCE = item.SpecialDefence;
                    mvm.SPEED          = item.Speed;
                    vms.Add(mvm);
                }
                ProductViewModel[] myCat = vms.ToArray();
                HttpContext.Session.SetObject(SessionVars.Catalogue, myCat);
            }
            vm.SetBrands(HttpContext.Session.GetObject <List <Brand> >(SessionVars.Brands));
            return(View("Index", vm));
        }