Пример #1
0
        public IActionResult AddHamper()
        {
            var product       = _productService.GetAll().Select(p => p.ProductName).ToList();
            var productChecks = product.Select(p => new ProductCheckList
            {
                ProductName = p,
                Checked     = false
            }).ToList();

            var cats      = _categoryService.GetAll().ToList();
            var filenames = _imageService.GetAll().ToList();
            var catSelect = cats.Select(x => new SelectListItem
            {
                Value = x.CategoryId.ToString(),
                Text  = x.CategoryName
            });
            var fileSelect = filenames.Select(x => new SelectListItem
            {
                Value = x.ImageId.ToString(),
                Text  = x.FileName
            });

            AdminAddHamperViewModel vm = new AdminAddHamperViewModel
            {
                ProductNamesList  = productChecks.ToArray(),
                CategoryNamesList = catSelect.ToList(),
                FileNames         = fileSelect.ToList()
            };

            return(View(vm));
        }
Пример #2
0
        public async Task <IActionResult> AddHamper(AdminAddHamperViewModel vm)
        {
            if (ModelState.IsValid)
            {
                var search = _hamperService.GetSingle(h => h.HamperName == vm.HamperName);
                if (search != null)
                {
                    ModelState.AddModelError("", "Hamper Already Exists");
                    return(View(vm));
                }
                bool isCId = int.TryParse(vm.CategoryId, out int categoryid);
                bool isIId = int.TryParse(vm.ImageId, out int imageid);

                if (!isCId && !isIId)
                {
                    return(NotFound());
                }
                var getnames   = vm.ProductNamesList.Where(pl => pl.Checked == true).Select(p => p.ProductName);
                var productids = _productService.Query(p => getnames.Any(g => g == p.ProductName))
                                 .Select(it => it.ProductId);

                IEnumerable <HamperProduct> hamperProducts = productids.Select(p => new HamperProduct
                {
                    ProductId = p
                });

                await _hamperService.Create(new Hamper
                {
                    HamperName     = vm.HamperName,
                    Cost           = vm.Cost,
                    ImageId        = imageid,
                    CategoryId     = categoryid,
                    HamperProducts = hamperProducts.ToList()
                });


                return(RedirectToAction("Index", "Admin"));
            }
            return(View(vm));
        }