Пример #1
0
        public ActionResult UpdateProduct(FormCollection form)
        {
            try
            {
                int     Id            = int.Parse(form["Id"]);
                string  productName   = Convert.ToString(form["ProductName"]);
                string  productInfo   = Convert.ToString(form["Description"]);
                double  productPrice  = double.Parse(form["NewPrice"]);
                int     statusProduct = int.Parse(form["statusProduct_change"]);
                var     listimg       = form["listimgdelete"].ToString();
                var     listimgdelete = JsonConvert.DeserializeObject <dynamic>(listimg);
                Product product       = db.Products.Single(p => p.Id == Id);
                product.ProductName = productName;
                product.OldPrice    = product.NewPrice;
                product.NewPrice    = productPrice;
                product.Description = productInfo;
                product.Status      = statusProduct;
                product.DateUpdate  = DateTime.Now;
                foreach (int item in listimgdelete)
                {
                    FileAttach fi = new FileAttach {
                        Id = item
                    };
                    db.Entry(fi).State = EntityState.Deleted;
                }
                for (int i = 0; i < Request.Files.Count; i++)
                {
                    HttpPostedFileBase file = Request.Files[i];

                    var    fileName         = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + Path.GetFileName(file.FileName);
                    var    orginalDirectory = new DirectoryInfo(Server.MapPath("~/Upload/Image"));
                    string pathString       = Path.Combine(orginalDirectory.ToString(), "");
                    var    path             = string.Format("{0}\\{1}", pathString, fileName);
                    file.SaveAs(path);
                    FileAttach fileimg = new FileAttach
                    {
                        ProductId  = product.Id,
                        ImageLink  = "Upload/Image/" + fileName.ToString(),
                        DateCreate = DateTime.Now,
                        Status     = 1
                    };
                    db.FileAttaches.Add(fileimg);
                }
                db.SaveChanges();
                return(Json(new { success = true, mess = "Cập nhật thông tin sản phẩm " + productName + " thành công!" }, JsonRequestBehavior.AllowGet));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Пример #2
0
 public ActionResult AddProduct(FormCollection form)
 {
     try
     {
         int     gender        = int.Parse(form["radio"]);
         int     categoryId    = int.Parse(form["ValueParentCategory"]);
         string  productName   = Convert.ToString(form["ProductName"]);
         string  productInfo   = Convert.ToString(form["Description"]);
         double  productPrice  = double.Parse(form["NewPrice"]);
         int     statusProduct = int.Parse(form["statusProduct"]);
         Product product       = new Product
         {
             CategoryId  = categoryId,
             ProductName = productName,
             Description = productInfo,
             NewPrice    = productPrice,
             DateCreate  = DateTime.Now,
             Status      = statusProduct
         };
         db.Products.Add(product);
         for (int i = 0; i < Request.Files.Count; i++)
         {
             HttpPostedFileBase file = Request.Files[i];
             if (file.FileName != "" && file.FileName != null)
             {
                 var    fileName         = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + Path.GetFileName(file.FileName);
                 var    orginalDirectory = new DirectoryInfo(Server.MapPath("~/Upload/Image"));
                 string pathString       = Path.Combine(orginalDirectory.ToString(), "");
                 var    path             = string.Format("{0}\\{1}", pathString, fileName);
                 file.SaveAs(path);
                 FileAttach fileimg = new FileAttach
                 {
                     ProductId  = product.Id,
                     ImageLink  = "Upload/Image/" + fileName.ToString(),
                     DateCreate = DateTime.Now,
                     Status     = 1
                 };
                 db.FileAttaches.Add(fileimg);
             }
         }
         db.SaveChanges();
         return(Json(new { success = true, mess = "Thêm sản phẩm " + productName + " thành công!" }, JsonRequestBehavior.AllowGet));
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Пример #3
0
        private bool saveProduct()
        {
            if (validateForm())
            {
                _product.Name        = txbNameProduct.Text;
                _product.Code        = txbCodeProduct.Text;
                _product.Observation = txbObservation.Text;
                _product.IsActive    = cbxIsActive.Checked;

                if (rbtAsInput.Checked)
                {
                    _product.AsInput = true;
                }
                else if (rbtAsSale.Checked)
                {
                    _product.AsSale = true;
                }
                else
                {
                    // MessageBox.Show("Marque el tipo de producto!");
                }

                _product.UnitPrice       = Convert.ToDouble(nudProductPrice.Value);
                _product.UnitInStock     = Convert.ToInt32(nudUnitStock.Value);
                _product.QuantityPerUnit = Convert.ToInt32(nudQuantityUnit.Value);
                _product.UnitOrders      = Convert.ToInt32(nudUnitOrder.Value);

                using (var MyDbEntities = new AppDBContext())
                {
                    if (_product.ProductId == 0)
                    {
                        try
                        {
                            MyDbEntities.Product.Add(_product);
                            MyDbEntities.SaveChanges();

                            // add product-categories relationship
                            ProductCategory categories = new ProductCategory();
                            int             idcategori = Convert.ToInt32(cbxCategories.SelectedValue);
                            if (idcategori > 0)
                            {
                                categories.CategoryId = idcategori;
                                categories.ProductId  = _product.ProductId;
                            }

                            _product.Categories = new List <ProductCategory>();
                            _product.Categories.Add(categories);
                            MyDbEntities.SaveChanges();

                            if (!openFile.FileName.Equals(""))
                            {
                                if (openFile.CheckFileExists)
                                {
                                    var fileName = $"{Guid.NewGuid().ToString()}{Path.GetExtension(openFile.FileName)}";

                                    string fullPath = Path.GetDirectoryName(Application.ExecutablePath);
                                    fullPath = fullPath.Remove(fullPath.IndexOf("\\bin\\Debug"));
                                    string filePath = fullPath + AppConsts.DefaultPathFolderImage + fileName;
                                    System.IO.File.Copy(openFile.FileName, filePath);

                                    FileAttach file = new FileAttach();
                                    file.FileName = fileName;
                                    file.Path     = filePath;
                                    file.Type     = FileType.ProductImage;
                                    var fileInfo = new FileInfo(filePath);
                                    file.Size     = fileInfo.Length;
                                    file.IsActive = true;
                                    //file.ProductId = _product.ProductId;
                                    MyDbEntities.FileAttach.Add(file);
                                    MyDbEntities.SaveChanges();

                                    _product.FileAttaches = new List <FileAttach>();
                                    _product.FileAttaches.Add(file);
                                    MyDbEntities.SaveChanges();
                                }
                            }


                            MyDbEntities.SaveChanges();
                            return(true);
                        }
                        catch (Exception exc)
                        {
                            var msj = exc.Message.ToString();
                            return(false);
                        }
                    }
                    else
                    {
                        return(false);
                        //MyDbEntities.Entry(_product).State = EntityState.Modified;
                        //MyDbEntities.SaveChanges();
                        //_product.ProductId = 0;
                        //MessageBox.Show("Information has been Updated", "Modified", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }

            return(false);
        }