Exemplo n.º 1
0
        public async Task <IActionResult> AddBrand2(int id)
        {
            SpareCategoryList scl = new SpareCategoryList();

            var model = await _context.SpareCategories.FindAsync(id);

            var allbrands = _context.SpareBrands.Select(sb => new SpareBrandList2()
            {
                Id_Brand        = sb.Id,
                NameBrand       = sb.Name,
                ImageUrlBrand   = sb.ImageUrl,
                Is_checkedBrand = sb.BrandCategories.Any(bc => bc.SpareCategory.Id == id) ? true : false
            })
                            .OrderBy(sb => sb.NameBrand)
                            .OrderByDescending(sb => sb.Is_checkedBrand)
                            .ToList();

            scl.NameCategory   = model.Name;
            scl.ImgUrlCategory = model.ImageUrl;
            scl.IdCategory     = model.Id;
            scl.SpareBrands    = allbrands;


            return(View(scl));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> AddBrand2(SpareCategoryList model)
        {
            if (model.SpareBrands != null)
            {
                List <BrandCategoryEntity> brandCategory = new List <BrandCategoryEntity>();

                foreach (var item in model.SpareBrands)
                {
                    if (item.Is_checkedBrand == true)
                    {
                        brandCategory.Add(new BrandCategoryEntity()
                        {
                            SpareBrand    = await _context.SpareBrands.FindAsync(item.Id_Brand),
                            SpareCategory = await _context.SpareCategories.FindAsync(model.IdCategory)
                        });
                    }
                }

                var table = _context.BrandCategories.Where(bc => bc.SpareCategory.Id == model.IdCategory);
                var list  = table.Except(brandCategory).ToList();

                foreach (var item in list)
                {
                    _context.BrandCategories.Remove(item);
                    await _context.SaveChangesAsync();
                }

                table = _context.BrandCategories.Where(bc => bc.SpareCategory.Id == model.IdCategory);
                foreach (var item in brandCategory)
                {
                    if (!table.Contains(item))
                    {
                        await _context.BrandCategories.AddAsync(item);

                        await _context.SaveChangesAsync();
                    }
                }
            }

            return(RedirectToAction($"Details/{model.IdCategory}"));
        }