Esempio n. 1
0
        public async Task <IActionResult> Update(ProductFormViewModel viewModel)
        {
            if (viewModel.categoryList.Count == 0)
            {
                TempData["MSG_E"] = Message.MSG_E_005;
                ICollection <Category> list = await _categoryRepository.FindAllsAsync(_employeeLogin.GetEmployee().BusinessId);

                ViewBag.Category = list.Select(x => new SelectListItem(x.Name, x.Id.ToString()));
                return(View(nameof(Details), viewModel));
            }

            if (ModelState.IsValid)
            {
                viewModel.Product.UpdateDate = DateTime.Now;
                foreach (var item in viewModel.categoryList)
                {
                    viewModel.Product.CategoryProduct.Add(new CategoryProduct {
                        CategoryId = item, ProductId = viewModel.Product.Id
                    });
                }
                List <CategoryProduct> categoryProductsDB = _categoryProductRepository.FindAllsProdut(viewModel.Product.Id).ToList();
                _categoryProductRepository.Remove(categoryProductsDB);
                _categoryProductRepository.Insert(viewModel.Product.CategoryProduct.ToList());
                _productRepository.Update(viewModel.Product);
                TempData["MSG_S"] = Message.MSG_S_001;
                return(RedirectToAction(nameof(Details), new { Id = viewModel.Product.Id }));
            }
            ICollection <Category> listCat = await _categoryRepository.FindAllsAsync(_employeeLogin.GetEmployee().BusinessId);

            ViewBag.Category = listCat.Select(x => new SelectListItem(x.Name, x.Id.ToString()));
            return(View(nameof(Details), viewModel));
        }