Ejemplo n.º 1
0
        public ActionResult Edit([Bind(Include = "id_product,name,id_manufacturer,id_category_default,type,on_sale,quantity,price,additional_shipping_cost,reference,width,height,depth,weight,quantity_discount,available_date,date_add,date_upd,description_short,description")] Product product)
        {
            if (ModelState.IsValid)
            {
                //if (!String.IsNullOrEmpty(id_category_selected))
                {
                    var d = db.Categories.Where(x => x.name == id_category_selected).Select(x => x.id_category).SingleOrDefault();
                    //  (from x in db.Categories
                    //              where x.name == id_category_selected
                    //            select x.id_category);
                    product.id_category_default = d;
                }

                db.Entry(product).State = EntityState.Modified;
                db.SaveChanges();

                var g = db.Categories_Products.Where(x => x.id_product == product.id_product).AsEnumerable().ToList();
                db.Categories_Products.RemoveRange(g);
                db.SaveChanges();

                if (list.Count() != 0)
                {
                    foreach (var item in list)
                    {
                        if (!String.IsNullOrEmpty(item.ToString()))
                        {
                            Category_Product cp = new Category_Product();
                            cp.id_product  = product.id_product;
                            cp.id_category = (int)(from c in db.Categories
                                                   where c.name == item.ToString()
                                                   select c.id_category)
                                             .Single();
                            int jj = (from c in db.Categories_Products
                                      where c.id_category == cp.id_category && c.id_product == cp.id_product
                                      select new { c.id_category, c.id_product }).ToList().Count();

                            if (jj == 0)
                            {
                                db.Categories_Products.Add(cp);
                                db.SaveChanges();
                            }

                            else
                            {
                                TempData["msg"] = "<script>alert('Error: Category already added');</script>";
                                //list = null;
                                //return RedirectToAction("Edit", "Products", new { id = product.id_product });
                            }

                            //dt.Tables.Rows.Add(cp);
                            //dt.Rows.Add(cp);
                        }
                    }
                    list = null;
                }

                return(RedirectToAction("Index"));
            }
            return(View(product));
        }
Ejemplo n.º 2
0
        public ActionResult ThemDanhMuc(FormCollection collection, HttpPostedFileBase imageCategory)
        {
            if (Session["idAccount"] == null)
            {
                return(RedirectToAction("DangNhap", "Login"));
            }
            if (!Equals(Session["idRole"], 5))
            {
                return(HttpNotFound());
            }
            var ma   = collection["id"];
            var ten  = collection["name"];
            var icon = collection["icon"];


            var csdt = from lg in db.Category_Product
                       select lg;

            foreach (var checksdt in csdt)
            {
                if (checksdt.idCategory_Product == Int32.Parse(ma.ToString()))
                {
                    ViewData["Err55"] = "Mã này đã tồn tại trong hệ thống!";
                    return(View());
                }
                else if (checksdt.nameCategory == ten)
                {
                    ViewData["Err55"] = "Tên này đã tồn tại trong hệ thống!";
                    return(View());
                }
            }
            if (String.IsNullOrEmpty(ten))
            {
                ViewData["Err5"] = "Vui lòng nhập Tên danh mục!";
            }
            else if (String.IsNullOrEmpty(ma))
            {
                ViewData["Err5"] = "VUi lòng chọn Mã danh mục!";
            }
            else if (String.IsNullOrEmpty(icon))
            {
                ViewData["Err5"] = "Vui lòng nhập Icon danh mục!";
            }
            else
            {
                Category_Product info = new Category_Product();
                if (ModelState.IsValid)
                {
                    string extension1 = System.IO.Path.GetExtension(imageCategory.FileName);
                    if (Equals(extension1, ".png") || Equals(extension1, ".jpg"))
                    {
                        var filename1 = DateTime.Now.ToString("ddMMyyyyHHmmss-") + Guid.NewGuid().ToString() + ".jpg";
                        var path1     = Path.Combine(Server.MapPath("~/Assets/ImageCategory"), filename1);

                        if (System.IO.File.Exists(path1))
                        {
                            ViewData["Err55"] = "(*) Hình ảnh đã tồn tại !";
                        }
                        else
                        {
                            imageCategory.SaveAs(path1);
                            info.imageCategory      = filename1;
                            info.idCategory_Product = Int32.Parse(ma.ToString());
                            info.iconCategory       = icon;
                            info.nameCategory       = ten;
                            info.iconCategory       = icon;
                            db.Category_Product.Add(info);
                        }
                        db.SaveChanges();
                    }
                }
                ;
            }
            return(RedirectToAction("DanhMucSanPham", "Manager"));
        }