示例#1
0
        public async Task <IActionResult> CreateSubmit(CreateProductSubmitVm model, IFormFile Image)
        {
            var category = this.dbContext.Categories.FirstOrDefault(cat => cat.Id == model.CategoryId);

            //var scatId = Guid.Parse("3C4C483C-30E9-4F11-BEA3-B5FBC288C7AE");
            //var symcaId = Guid.Parse("030be0f0-6d44-43a0-b278-df478caffbad");
            ////  var shampId = Guid.Parse("23a2a828-3ba6-4403-a068-92168f0b02cb");
            //var shampMyloId = Guid.Parse("0E74B97F-B482-4919-B8CC-7713A4283104");
            //var number = this.dbContext.Products.Where(p => p.CategoryId == category.Id).Count();
            //if (category.Id == scatId)
            //{
            //    number = number + 500;
            //}
            ////else if (category.Id == shampId)
            ////{
            ////    number = number + 1000;
            ////}
            //else if (category.Id == shampMyloId)
            //{
            //    number = number + 10000;
            //}

            var newProduct = new Product()
            {
                Id              = Guid.NewGuid(),
                Name            = model.Name,
                PriceGoods      = model.PriceGoods,
                Number          = this.GenerateNumber(), // number + 1,//  (number + 1).ToString(), //
                CategoryId      = category.Id,
                Discription     = model.Discription,
                DiscriptionFull = model.DiscriptionFull,
                DateCreate      = DateTime.Now
            };

            if (Image != null)
            {
                string     falename   = Image.FileName;
                string     path       = $"/files/{falename}";
                string     serverPath = $"{this.environment.WebRootPath}{path}";
                FileStream fs         = new FileStream(serverPath, FileMode.Create,
                                                       FileAccess.Write);
                await Image.CopyToAsync(fs);

                fs.Close();

                newProduct.Image = path;
            }

            this.dbContext.Products.Add(newProduct);
            this.dbContext.SaveChanges();

            return(RedirectToAction(nameof(ProductListForAdmin), new { categoryId = newProduct.CategoryId }));
        }
示例#2
0
        public IActionResult Create()
        {
            var model = new CreateProductSubmitVm()
            {
                Categories = this.dbContext.Categories.Select(cat => new SelectListItem
                {
                    Value = cat.Id.ToString(),
                    Text  = cat.Name
                }).ToList()
            };

            return(View(model));
        }