public ActionResult Edit(DiscountFormViewModel newDiscount) { if (ModelState.IsValid) { #region Removing All Previous Discounts var prevDicounts = _repo.GetDiscountsByGroupIdentifier(newDiscount.GroupIdentifier); foreach (var item in prevDicounts) { _repo.Delete(item.Id); } #endregion var groupIdentifier = Guid.NewGuid().ToString(); #region Adding Brands Discounts if (newDiscount.BrandIds != null) { foreach (var item in newDiscount.BrandIds) { var discount = new Discount() { DiscountType = newDiscount.DiscountType, Amount = newDiscount.Amount, OfferId = newDiscount.OfferId, Title = newDiscount.Title, BrandId = item, GroupIdentifier = groupIdentifier }; _repo.Add(discount); } } #endregion #region Adding ProductGroups Discounts if (newDiscount.ProductGroupIds != null) { foreach (var item in newDiscount.ProductGroupIds) { var discount = new Discount() { DiscountType = newDiscount.DiscountType, Amount = newDiscount.Amount, OfferId = newDiscount.OfferId, Title = newDiscount.Title, ProductGroupId = item, GroupIdentifier = groupIdentifier }; _repo.Add(discount); } } #endregion #region Adding Products Discounts if (newDiscount.ProductIds != null) { foreach (var item in newDiscount.ProductIds) { var discount = new Discount() { DiscountType = newDiscount.DiscountType, Amount = newDiscount.Amount, OfferId = newDiscount.OfferId, Title = newDiscount.Title, ProductId = item, GroupIdentifier = groupIdentifier }; _repo.Add(discount); } } #endregion return(RedirectToAction("Index")); } ViewBag.Offers = _offerRepo.GetAll(); ViewBag.Brands = _brandRepo.GetAll(); ViewBag.ProductGroups = _productGroupRepo.GetAll(); ViewBag.Products = _productRepo.GetAll(); return(View()); }