Exemplo n.º 1
0
 public ActionResult Edit(Account account)
 {
     if (ModelState.IsValid)
     {
         db.Entry(account).State = EntityState.Modified;
         var entry = db.Entry(account);
         entry.Property(e => e.Email).IsModified    = false;
         entry.Property(e => e.Password).IsModified = false;
         if (account.Avatar == null)
         {
             entry.Property(e => e.Avatar).IsModified = false;
         }
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(account));
 }
Exemplo n.º 2
0
        public async Task <ActionResult> Edit(Order order)
        {
            if (ModelState.IsValid)
            {
                db.Entry(order).State = EntityState.Modified;
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(order));
        }
 public ActionResult Edit(Category category)
 {
     if (ModelState.IsValid)
     {
         category.ParentId        = (category.ParentId == null) ? 0 : category.ParentId;
         category.CreateDate      = DateTime.Now;
         category.Alias           = ConvertToAlias.ConvertTitle(category.CategoryName);
         db.Entry(category).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(category));
 }
Exemplo n.º 4
0
        public ActionResult Edit(/*[Bind(Include = "ProductId,ProductName,Description,CategoryId,Price,Image,Qty,Status")] */ Product product, HttpPostedFileBase Image)
        {
            // Lấy tổng sản phẩm
            var tags_size  = Request.Form["tags_size"].Split(',');
            var tags_color = Request.Form["tags_color"].Split(',');
            int Qty_detail = 0;

            for (int i = 0; i < tags_color.Length; i++)
            {
                for (int j = 0; j < tags_size.Length; j++)
                {
                    if (Request.Form["qty_detail[" + tags_color[i] + "-" + tags_size[j] + "]"] != null)
                    {
                        try
                        {
                            Qty_detail += Int16.Parse(Request.Form["qty_detail[" + tags_color[i] + "-" + tags_size[j] + "]"]);
                        }
                        catch
                        {
                            ModelState.AddModelError("ProductDetails", "Số lượng nhập ko hợp lệ");
                        }
                    }
                }
            }
            if (ModelState.IsValid)
            {
                db.Entry(product).State = EntityState.Modified;

                // Xóa chi tiết sản phẩm cũ
                var delete = db.ProductDetail.Where(x => x.ProductId == product.ProductId);
                db.ProductDetail.RemoveRange(delete);
                db.SaveChanges();

                // Lưu chi tiết sản phẩm
                product.Qty = (Qty_detail == 0) ? product.Qty : Qty_detail;
                for (int i = 0; i < tags_color.Length; i++)
                {
                    for (int j = 0; j < tags_size.Length; j++)
                    {
                        if (Request.Form["qty_detail[" + tags_color[i] + "-" + tags_size[j] + "]"] != null && Request.Form["price_detail[" + tags_color[i] + "-" + tags_size[j] + "]"] != null)
                        {
                            db.ProductDetail.Add(new ProductDetail()
                            {
                                ProductId = product.ProductId,
                                Color     = tags_color[i],
                                Size      = tags_size[j],
                                Qty       = (Request.Form["qty_detail[" + tags_color[i] + "-" + tags_size[j] + "]"] != null) ? Int16.Parse(Request.Form["qty_detail[" + tags_color[i] + "-" + tags_size[j] + "]"]) : 0,
                                Price     = (Request.Form["price_detail[" + tags_color[i] + "-" + tags_size[j] + "]"] != null) ? Decimal.Parse(Request.Form["price_detail[" + tags_color[i] + "-" + tags_size[j] + "]"]) : 0
                            });
                        }
                    }
                }
                db.SaveChanges();

                //Lưu ảnh detail sản phẩm
                List <string> listFileName = new List <string>();
                if (Session["fileUpload"] != null)
                {
                    fileUpload = (List <HttpPostedFileBase>)Session["fileUpload"];
                    foreach (var item in fileUpload)
                    {
                        if (item != null && item.ContentLength > 0)
                        {
                            string extensionFile2 = item.FileName.Substring(item.FileName.LastIndexOf("."));
                            string newfilename2   = Common.EncryptMD5(DateTime.Now.ToBinary().ToString()) + extensionFile2;
                            db.ProductImage.Add(new ProductImage()
                            {
                                ImageName = newfilename2,
                                ProductId = product.ProductId
                            });
                            string path2 = Path.Combine(Server.MapPath("~/Areas/Admin/Content/Images/ProductImages"), newfilename2);
                            item.SaveAs(path2);
                            listFileName.Add(newfilename2);
                        }
                    }
                }
                Session["fileUpload"] = null;

                //Xóa ảnh detail người dùng xóa
                if (fileNameTemp != null)
                {
                    foreach (var item in fileNameTemp)
                    {
                        var rs = db.ProductImage.FirstOrDefault(x => x.ImageName == item);
                        if (rs != null)
                        {
                            db.ProductImage.Remove(rs);
                        }
                        string fullpath = Request.MapPath("~/Areas/Admin/Content/Images/ProductImages/" + item);
                        if (System.IO.File.Exists(fullpath))
                        {
                            System.IO.File.Delete(fullpath);
                        }
                    }
                }
                db.SaveChanges();

                product.Image = db.ProductImage.Where(x => x.ProductId == product.ProductId).Select(x => x.ImageName).FirstOrDefault();

                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            ViewBag.CategoryId = new SelectList(db.Category, "CategoryId", "CategoryName", product.CategoryId);
            return(View(product));
        }
        public ActionResult Edit([Bind(Include = "ProductId,ProductName,Description,CategoryId,Price,Image,Qty,Status")] Product product, HttpPostedFileBase Image)
        {
            if (ModelState.IsValid)
            {
                db.Entry(product).State = EntityState.Modified;
                //Nếu người dùng thay đổi Image
                //if (Image != null && Image.ContentLength > 0)
                //{
                //    //Lấy tên file image cũ
                //    KidShopDbContext db2 = new KidShopDbContext();
                //    var currentImage = db2.Product.FirstOrDefault(x => x.ProductId == product.ProductId).Image;
                //    db2.Dispose();

                //    //Xóa image cũ
                //    string fullpath = Request.MapPath("~/Areas/Admin/Content/Images/Product/" + currentImage);
                //    if (System.IO.File.Exists(fullpath))
                //    {
                //        System.IO.File.Delete(fullpath);
                //    }

                //    //Lưu file image mới vào folder
                //    string extensionFile = Image.FileName.Substring(Image.FileName.LastIndexOf("."));
                //    string newfilename = Common.EncryptMD5(DateTime.Now.ToBinary().ToString()) + extensionFile;
                //    string path = Path.Combine(Server.MapPath("~/Areas/Admin/Content/Images/Product"), Path.GetFileName(newfilename));
                //    Image.SaveAs(path);
                //    product.Image = newfilename;
                //}
                //else
                //{
                //    //Nếu người dùng ko chọn Image thì Image sẽ giữ nguyên ko thay đổi
                //    db.Entry(product).Property(e => e.Image).IsModified = false;
                //}

                //Lưu ảnh detail sản phẩm
                List <string> listFileName = new List <string>();
                if (Session["fileUpload"] != null)
                {
                    fileUpload = (List <HttpPostedFileBase>)Session["fileUpload"];
                    foreach (var item in fileUpload)
                    {
                        if (item != null && item.ContentLength > 0)
                        {
                            string extensionFile2 = item.FileName.Substring(item.FileName.LastIndexOf("."));
                            string newfilename2   = Common.EncryptMD5(DateTime.Now.ToBinary().ToString()) + extensionFile2;
                            db.ProductImage.Add(new ProductImage()
                            {
                                ImageName = newfilename2,
                                ProductId = product.ProductId
                            });
                            string path2 = Path.Combine(Server.MapPath("~/Areas/Admin/Content/Images/ProductImages"), newfilename2);
                            item.SaveAs(path2);
                            listFileName.Add(newfilename2);
                        }
                    }
                }
                Session["fileUpload"] = null;

                //Xóa ảnh detail người dùng xóa
                if (fileNameTemp != null)
                {
                    foreach (var item in fileNameTemp)
                    {
                        var rs = db.ProductImage.FirstOrDefault(x => x.ImageName == item);
                        if (rs != null)
                        {
                            db.ProductImage.Remove(rs);
                        }
                        string fullpath = Request.MapPath("~/Areas/Admin/Content/Images/ProductImages/" + item);
                        if (System.IO.File.Exists(fullpath))
                        {
                            System.IO.File.Delete(fullpath);
                        }
                    }
                }
                db.SaveChanges();
                product.Image = db.ProductImage.Where(x => x.ProductId == product.ProductId).Select(x => x.ImageName).FirstOrDefault();

                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            ViewBag.CategoryId = new SelectList(db.Category, "CategoryId", "CategoryName", product.CategoryId);
            return(View(product));
        }