Ejemplo n.º 1
0
        public JsonResult AddToBasket(string adName, int quantity)
        {
            db.ChangeDatabase(null, null, "customer", "pass");
            var userID = db.Users.FirstOrDefault(x => x.email == User.Identity.Name).id;
            var basket = db.Baskets.Where(x => x.adName == adName && x.userID == userID).ToList();

            if (basket.Count() != 0)
            {
                basket.First().quantity += quantity;

                db.Entry(basket.First()).State = EntityState.Modified;
                db.SaveChanges();
            }
            else
            {
                var newBasket = new Basket()
                {
                    adName   = adName,
                    userID   = userID,
                    quantity = quantity,
                };

                basketsDao.InsertBasket(newBasket);
            }

            return(Json(quantity));
        }
        public ActionResult Create(Basket basket)
        {
            if (ModelState.IsValid)
            {
                var insertedBasket = basketDao.InsertBasket(basket);
                if (insertedBasket != null)
                {
                    return(RedirectToAction("Index"));
                }
            }

            ViewBag.adName = new SelectList(db.Ads, "name", "description", basket.adName);
            ViewBag.userID = new SelectList(db.Users, "id", "name", basket.userID);
            return(View(basket));
        }