コード例 #1
0
        public ActionResult OnCreate(ProductModels product)
        {
            if (ModelState.IsValid)
            {
                if (string.IsNullOrWhiteSpace(product.Code))
                {
                    product.Code = string.Format("{0}-{1}", ConfigHelper.ProductCode, DateTime.Now.Ticks.GetHashCode());
                }
                product.ImageUrl = product.Image != null?
                                   product.Image.Upload() :
                                       product.ImageUrl;

                var newProduct = new Product
                {
                    Name         = product.Name,
                    Type         = product.CategoryId,
                    Summary      = product.Summary,
                    Code         = product.Code,
                    Image        = product.ImageUrl,
                    BrandId      = product.BrandId,
                    Country      = product.Country,
                    Store        = product.Store,
                    Manufacturer = product.ManufacturerId,
                    AccountId    = 0,
                    Number       = product.Number,
                    Unit1        = product.Unit,
                    Weight       = product.Weight,
                    Quantity     = product.Quantity,
                    Npp          = Convert.ToDouble(product.Npp),
                    Width        = product.Width,
                    Height       = product.Height,
                    Depth        = product.Depth,
                    Detail       = product.Detail,
                    WarrantyTime = product.WarrantyTime,
                    ExpireDate   = product.ExpireDate,
                    Title        = product.MetaTitle,
                    Keyword      = product.MetaKeyword,
                    Description  = product.MetaDescription,
                    Price        = product.Price,
                    Transport1   = product.Transport1,
                    Transport2   = product.Transport2,
                    Status       = false
                };
                var result = ProductService.Insert(newProduct);

                //Tao cac anh Slide va properties
                if (product.ProductImages != null && product.ProductImages.Length > 0)
                {
                    ProductImageService.Insert(newProduct.ID, product.Name, product.ProductImages);
                }
                //Tao cac gia tri thuoc tinh
                if (product.AttrLabel != null && product.AttrLabel.Length > 0)
                {
                    ProductAttributeService.Insert(newProduct.ID, product.AttrLabel, product.AttrDesc);
                }
                //Tao saleOff
                if (!string.IsNullOrWhiteSpace(product.Percent))
                {
                    ProductSaleOffService.Insert(new ProductSaleOff
                    {
                        ProductID = newProduct.ID,
                        Percent   = Convert.ToDouble(product.Percent),
                        StartTime = Convert.ToDateTime(product.StartDate),
                        EndTime   = Convert.ToDateTime(product.EndDate)
                    });
                }
                //Tao kich co
                if (product.Size != null && product.Size.Length > 0)
                {
                    ProductSizeService.Insert(newProduct.ID, product.Size);
                }
                //Tao mau sac
                if (product.Color != null && product.Color.Length > 0)
                {
                    ProductColorService.Insert(newProduct.ID, product.Color);
                }
                SetFlashMessage(result == Result.Ok?
                                $"Thêm sản phẩm '{product.Name}' thành công.":
                                "Lỗi khi thêm mới sản phẩm");
                if (product.SaveList)
                {
                    return(RedirectToAction("Index"));
                }
                ViewBag.ListCategory     = BuildListCategory();
                ViewBag.ListBrand        = ProductBrandService.GetAll();
                ViewBag.ListCountry      = CountryService.GetAll();
                ViewBag.ListStore        = ProductStoreService.GetAll();
                ViewBag.ListManufacturer = ProductManufacturerService.GetAll();
                ViewBag.ListUnit         = UnitService.GetAll();
                ViewBag.ListColor        = BuildListColor(product.Color);
                ViewBag.ListSize         = BuildListSize(product.Size);
                ViewBag.ListHour         = DataHelper.ListHour;
                ModelState.Clear();
                return(View("Create", product.ResetValue()));
            }
            ViewBag.ListCategory     = BuildListCategory();
            ViewBag.ListBrand        = ProductBrandService.GetAll();
            ViewBag.ListCountry      = CountryService.GetAll();
            ViewBag.ListStore        = ProductStoreService.GetAll();
            ViewBag.ListManufacturer = ProductManufacturerService.GetAll();
            ViewBag.ListUnit         = UnitService.GetAll();
            ViewBag.ListColor        = BuildListColor(product.Color);
            ViewBag.ListSize         = BuildListSize(product.Size);
            ViewBag.ListHour         = DataHelper.ListHour;

            return(View("Create", product));
        }