示例#1
0
        public ActionResult Details(int Id)
        {
            KataWebContext db      = new KataWebContext();
            var            product = db.Products.Find(Id);

            return(View(product));
        }
示例#2
0
        public ActionResult GetProducts(int categoryId, int?page, string search, string orderBy)
        {
            KataWebContext db       = new KataWebContext();
            var            products = db.Products.Where(x => x.CategoryId == categoryId).AsQueryable();

            if (string.IsNullOrEmpty(search) == false)
            {
                products = products.Where(p => p.Name.ToUpper().StartsWith(search.ToUpper()));
            }
            switch (orderBy)
            {
            case "NameDesc":
                products = products.OrderByDescending(y => y.Name);
                break;

            case "Price":
                products = products.OrderBy(y => y.Price);
                break;

            case "PriceDesc":
                products = products.OrderByDescending(y => y.Price);
                break;

            default:
                products = products.OrderBy(y => y.Name);
                break;
            }
            return(View(products.ToList().ToPagedList(page ?? 1, 3)));
        }
示例#3
0
        public ActionResult GetCategories()
        {
            KataWebContext db         = new KataWebContext();
            var            categories = db.Categories.ToList();

            return(PartialView(categories));
        }
示例#4
0
        public ActionResult GetRandomDiscountedProduct()
        {
            KataWebContext db            = new KataWebContext();
            var            products      = db.Products.Where(x => x.Discount == true).ToList();
            int            selectedIndex = (int)(DateTime.Now.Ticks % products.Count);
            var            product       = products[selectedIndex];

            return(PartialView(product));
        }
示例#5
0
        public ActionResult AddToCart(int Id)
        {
            KataWebContext db   = new KataWebContext();
            List <Item>    cart = (List <Item>)Session["cart"];

            if (cart == null)
            {
                //ako je null nista ne postoji u sessionu
                cart = new List <Item>();
                var  product = db.Products.Find(Id);
                Item item    = new Item();
                item.Product = product;
                item.Count   = 1;
                cart.Add(item);
                Session["cart"] = cart;
            }
            else
            {
                //lista postoji u sessionu
                //pretrazujem da li proizvod postoji u kosari
                int location = cart.FindIndex(x => x.Product.Id == Id);
                //nalazi index od elementa koji zadovoljava ovaj kriterij
                if (location == -1) //to znaci da proizvod nije pronadjen u listi
                {
                    var  product = db.Products.Find(Id);
                    Item item    = new Item();
                    item.Product = product;
                    item.Count   = 1;
                    cart.Add(item);
                    Session["cart"] = cart;
                }
                else
                { //proizvod nije pronadjen na lokaciji u varijabli location
                    cart[location].Count++;
                }
                Session["cart"] = cart;
            }
            return(RedirectToAction("Index"));
        }