コード例 #1
0
        public ActionResult AddItemToCart(string id, byte quantity, string type)
        {
            if (!Authentication.IsValid())
            {
                return(Json(new { Error = "Not Authenticated" }));
            }
            string name           = type == "SERVICO" ? ServiceDAO.GetById(Convert.ToUInt16(id)).Name : PackageDAO.GetById(Convert.ToUInt16(id)).Name;
            var    data           = CartDAO.GetList();
            bool   containsInCart = false;
            string view           = null;

            foreach (dynamic d in data)
            {
                if (d.Name == name)
                {
                    containsInCart = true;
                    break;
                }
            }
            CartDAO.InsertItem(name, quantity);
            if (!containsInCart)
            {
                if (type == "SERVICO")
                {
                    view = CustomHtmlHelper.CustomHtmlHelper.RenderPartialToString("Cart/_Service", new { Object = ServiceDAO.GetCartServiceByName(name) }, ControllerContext);
                }
                else
                {
                    view = CustomHtmlHelper.CustomHtmlHelper.RenderPartialToString("Cart/_Package", new { Object = PackageDAO.GetCartPackageByName(name) }, ControllerContext);
                }
            }

            JsonResult json = Json(new {
                contains     = containsInCart,
                itemQuantity = CartDAO.GetQuantity(name),
                price        = "R$ " + CartDAO.GetTotalPrice(),
                itemName     = name,
                htmlItem     = view,
                success      = "Adicionado!",
                count        = CartDAO.GetItemsCount()
            });

            json.MaxJsonLength = int.MaxValue;

            return(json);
        }