public ActionResult Edit(PromotionViewModel model) { if (ModelState.IsValid) { if (Request["Submit"] == "Save") { var Promotion = PromotionRepository.GetPromotionById(model.Id); AutoMapper.Mapper.Map(model, Promotion); Promotion.ModifiedUserId = WebSecurity.CurrentUserId; Promotion.ModifiedDate = DateTime.Now; PromotionRepository.UpdatePromotion(Promotion); List <PromotionDetail> promotionDetails = new List <PromotionDetail>(); if (model.DetailList == null) { model.DetailList = new List <PromotionDetailViewModel>(); } //lọc tất cả các dòng chi tiết phải có giá trị giảm giá model.DetailList = model.DetailList.Where(x => x.PercentValue > 0).ToList(); //nếu có các dòng danh mục đã được chọn thì lọc hết tất cá các dòng danh mục và sản phẩm không được chọn if (model.DetailList.Where(x => string.IsNullOrEmpty(x.CategoryCode) == false && x.Type == "product").Count() > 0) { model.DetailList.RemoveAll(x => (x.ProductId == null && string.IsNullOrEmpty(x.CategoryCode)) == true && x.Type == "product"); } else { //nếu ko có các dòng dnah mục được chọn thì chỉ cho phép tối đa một dòng danh mục và sản phẩm không chọn var categoryEmpty_productAll = model.DetailList.Where(x => (x.ProductId == null && string.IsNullOrEmpty(x.CategoryCode)) == true && x.Type == "product").ToList(); if (categoryEmpty_productAll.Count > 0) { model.DetailList.RemoveAll(x => (x.ProductId == null && string.IsNullOrEmpty(x.CategoryCode)) == true && x.Type == "product"); model.DetailList.Add(categoryEmpty_productAll.FirstOrDefault()); } } //nếu có các dòng danh mục đã được chọn thì lọc hết tất cá các dòng danh mục và dịch vụ không được chọn if (model.DetailList.Where(x => string.IsNullOrEmpty(x.CategoryCode) == false && x.Type == "service").Count() > 0) { model.DetailList.RemoveAll(x => (x.ProductId == null && string.IsNullOrEmpty(x.CategoryCode)) == true && x.Type == "service"); } else { //nếu ko có các dòng dnah mục được chọn thì chỉ cho phép tối đa một dòng danh mục và dịch vụ không chọn var categoryEmpty_productAll = model.DetailList.Where(x => (x.ProductId == null && string.IsNullOrEmpty(x.CategoryCode)) == true && x.Type == "service").ToList(); if (categoryEmpty_productAll.Count > 0) { model.DetailList.RemoveAll(x => (x.ProductId == null && string.IsNullOrEmpty(x.CategoryCode)) == true && x.Type == "service"); model.DetailList.Add(categoryEmpty_productAll.FirstOrDefault()); } } if (model.DetailList.Count != 0) { foreach (var item in model.DetailList.Where(x => x.ProductId == null)) { item.IsAll = true; } promotionDetails = model.DetailList.Select(x => new PromotionDetail { Id = x.Id, IsAll = x.IsAll, IsDeleted = false, PercentValue = x.PercentValue, ProductId = x.ProductId, CategoryCode = x.CategoryCode, QuantityFor = x.QuantityFor, PromotionId = Promotion.Id }).ToList(); } var detailListOld = PromotionRepository.GetAllPromotionDetailBy(model.Id).ToList(); var deleteList = detailListOld.Where(x => promotionDetails.Any(y => y.Id == x.Id) == false).ToList(); var updateList = detailListOld.Where(x => promotionDetails.Any(y => y.Id == x.Id)).ToList(); var addNewList = promotionDetails.Where(x => x.Id == 0).ToList(); //xóa các chi tiết không chọn nữa for (int i = 0; i < deleteList.Count; i++) { PromotionRepository.DeletePromotionDetailRs(deleteList[i].Id); } //cập nhật các chi tiết thay đổi for (int i = 0; i < updateList.Count; i++) { var detailUpateNewData = promotionDetails.Where(x => x.Id == updateList[i].Id).FirstOrDefault(); if (detailUpateNewData != null) { updateList[i].IsAll = detailUpateNewData.IsAll; updateList[i].PercentValue = detailUpateNewData.PercentValue; updateList[i].CategoryCode = detailUpateNewData.CategoryCode; updateList[i].ProductId = detailUpateNewData.ProductId; updateList[i].QuantityFor = detailUpateNewData.QuantityFor; } PromotionRepository.UpdatePromotionDetail(updateList[i]); } //thêm mới các chi tiết mới chọn for (int i = 0; i < addNewList.Count; i++) { PromotionRepository.InsertPromotionDetail(addNewList[i]); } TempData[Globals.SuccessMessageKey] = App_GlobalResources.Wording.UpdateSuccess; return(RedirectToAction("Index")); } return(View(model)); } return(View(model)); //if (Request.UrlReferrer != null) // return Redirect(Request.UrlReferrer.AbsoluteUri); //return RedirectToAction("Index"); }