public IActionResult createImages([Bind("SId", "ImgPrice", "ImgName", "ImgDescription", "IsDeal")] Simages images, IFormFile ImgPath, string img)
        {
            ViewBag.error = 0;
            try
            {
                if (ModelState.IsValid)
                {
                    if (img != null)
                    {
                        db.Simages.Add
                        (
                            new Simages
                        {
                            SId            = images.SId,
                            IsDeal         = images.IsDeal,
                            ImgPath        = img,
                            ImgName        = images.ImgName,
                            ImgPrice       = images.ImgPrice,
                            ImgDescription = images.ImgDescription
                        }

                        );
                    }
                    else
                    {
                        db.Simages.Add
                        (
                            new Simages
                        {
                            SId            = images.SId,
                            IsDeal         = images.IsDeal,
                            ImgPath        = uploadFile(ImgPath, (DateTime.Now.Day + DateTime.Now.Month + DateTime.Now.Year).ToString(), null),
                            ImgName        = images.ImgName,
                            ImgPrice       = images.ImgPrice,
                            ImgDescription = images.ImgDescription
                        }

                        );
                    }

                    db.SaveChanges();
                }
                else
                {
                    ViewBag.error = 3;
                }
            }
            catch (Exception)
            {
                ViewBag.error = 2;
            }
            return(View("AddImages", Services()));
        }
Example #2
0
        public IActionResult AddToCart(int id)
        {
            try
            {
                if (HttpContext.Session.GetString("Cart") != null)
                {
                    var list = JsonConvert.DeserializeObject <List <Simages> >(HttpContext.Session.GetString("Cart"));
                    if (list != null)
                    {
                        Simages simages = db.Simages.Find(id);
                        foreach (var item in list)
                        {
                            if (item.ImgId == id)
                            {
                                return(NoContent());
                            }
                        }

                        list.Add(db.Simages.Find(id));
                        HttpContext.Session.SetString("Cart", JsonConvert.SerializeObject(list));
                        return(Json(1));
                    }
                }
                else
                {
                    List <Simages> list = new List <Simages>();
                    list.Add(db.Simages.Find(id));
                    HttpContext.Session.SetString("Cart", JsonConvert.SerializeObject(list));
                    return(Json(1));
                }

                return(NoContent());
            }
            catch (Exception)
            {
                return(Forbid());
            }
        }