Example #1
0
        public ActionResult Add(Guid id)
        {
            Product eklenecekUrun = _productService.GetById(id);

            CartProductVM sepetUrunu = new CartProductVM
            {
                ID          = eklenecekUrun.ID,
                ProductName = eklenecekUrun.Name,
                UnitPrice   = eklenecekUrun.Price,
                Quantity    = 1
            };

            if (Session["sepet"] != null)
            {
                //Eğer sepet zaten varsa, sepeti yakala, gerçek tipine cast et, içine ürünü ekle, tekrar session içerisinde aynı yere son halini ekle.
                ProductCart cart = Session["sepet"] as ProductCart;
                cart.AddToCart(sepetUrunu);
                Session["sepet"] = cart;
            }
            else
            {
                //Eğer session içerisinde bir sepet yoksa, ilk kez sepet oluşuyordur. Sepeti oluştur ve ilk ürünü ekle.
                ProductCart cart = new ProductCart();
                cart.AddToCart(sepetUrunu);
                //İlk session oluşturmamda aşağıdaki kod kullanılır.
                //ilk parametre string tipte bu sessiondan tekrar yakalamakta kullancağımız isim(şu an sepet ismi verdik), ikinci parametre ise gerçek sepept nesnemizdir.
                Session.Add("sepet", cart);
            }
            //Ekleme işlemi sonucunda geriye json tipinde boş bile olsa değer dönmeliyiz. Eğer "return View()" bırakırsak , o zaman bize metodun kendi isminde bir view arayacak ve sayfamızı değiştirmeye çalışacaktır. Ajax sayesinde sayfanın yenilenmesini bile istemiyoruz. Bu nedenle bu metotlara bir view oluşturmuyoruz. Return View() yerine Return Json() yazıyoruz.
            ProductCart cart2 = Session["sepet"] as ProductCart;

            return(Json(cart2.CartProductList));
        }
Example #2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            string rawId = Request.QueryString["ProductId"];

            try
            {
                int productId        = int.Parse(rawId);
                var userShoppingCart = new ProductCart();
                userShoppingCart.AddToCart(productId);
            }
            catch
            {
                throw new Exception("ERROR: It is illegal to load AddToCart.aspx without setting a ProductId.");
            }
            Response.Redirect("ShoppingCart.aspx");
        }