private async Task AddAttributechanged(Product product) { List <Product_Attribut> productAttribut = new List <Product_Attribut>(); foreach (var atrgrp in await _attributeGrpService.GetByProductGroupId(product.ProductGroupId)) { { //finding product attribute if that is inserted before or not var find = await _productAttributeService.GetProductAttribute(product.ProductId, atrgrp.AttributGrpId); if (find != null) { if (Request.Form["Grp_" + atrgrp.AttributGrpId.ToString()] == "none") { _productAttributeService.Remove(find); } //if user clicked one of attribute of this grp else if (Request.Form["Grp_" + atrgrp.AttributGrpId.ToString()].Any()) { find.AttributItemId = Convert.ToInt32(Request.Form["Grp_" + atrgrp.AttributGrpId.ToString()]); _productAttributeService.Edit(find); } } else { if (Request.Form["Grp_" + atrgrp.AttributGrpId.ToString()].Any() && Request.Form["Grp_" + atrgrp.AttributGrpId.ToString()] != "none") { productAttribut.Add(new Product_Attribut() { AttributItemId = Convert.ToInt32(Request.Form["Grp_" + atrgrp.AttributGrpId.ToString()]), ProductId = product.ProductId, }); } } } } if (productAttribut != null) { _productAttributeService.Add(productAttribut); } }
public ActionResult Edit(Product products, string ProductGalleriesName, string tags) { if (ModelState.IsValid) { //------------Create Gallery Product -------------- List <ProductGallery> productGalleries = new List <ProductGallery>(); var Gallery = ProductGalleriesName.Split(','); for (int i = 0; i < Gallery.Length - 1; i++) { productGalleries.Add(new ProductGallery { ProductId = products.ProductId, ImageName = Gallery[i], }); } _productService.AddGalleries(productGalleries); //-------------------Tags--------------------- _productService.DeleteTagsByProduct(products.ProductId); if (!string.IsNullOrEmpty(tags)) { List <ProductTag> productTags = new List <ProductTag>(); foreach (string t in tags.Split('-')) { productTags.Add(new ProductTag { ProductId = products.ProductId, TagTitle = t.ToLowerInvariant().Trim() }); } _productService.AddTags(productTags); } //-----------------Attribute---------------------- List <Product_Attribut> productAttribut = new List <Product_Attribut>(); foreach (var atrgrp in _attributeGrpService.GetAttrGrpProductBase(products.ProductGroupId)) { { //finding product attribute if that is inserted before or not var find = _productAttributeService.GetProductAttribute(products.ProductId, atrgrp.AttributGrpId); if (find != null) { if (Request.Form["Grp_" + atrgrp.AttributGrpId.ToString()] == "none") { _productAttributeService.Delete(find); } //if user clicked one of attribute of this grp else if (Request.Form["Grp_" + atrgrp.AttributGrpId.ToString()] != null) { find.AttributItemId = Convert.ToInt32(Request.Form["Grp_" + atrgrp.AttributGrpId.ToString()]); _productAttributeService.Edit(find); } } else { if (Request.Form["Grp_" + atrgrp.AttributGrpId.ToString()] != null && Request.Form["Grp_" + atrgrp.AttributGrpId.ToString()] != "none") { productAttribut.Add(new Product_Attribut() { AttributItemId = Convert.ToInt32(Request.Form["Grp_" + atrgrp.AttributGrpId.ToString()]), ProductId = products.ProductId, }); } } } } if (productAttribut != null) { _productAttributeService.Add(productAttribut); } //-----------------Details---------------------- List <ProductDetail> productDetail = new List <ProductDetail>(); foreach (var detItm in _detailItemService.GetDetItemByProduct(products)) { var find = _productDetailService.GetProductDetail(products.ProductId, detItm.DetailItemId); if (find != null) { find.Value = Request.Form["detItem_" + detItm.DetailItemId.ToString()]; _productDetailService.Edit(find); } else { productDetail.Add(new ProductDetail { DetailItemId = detItm.DetailItemId, ProductId = products.ProductId, Value = Request.Form["detItem_" + detItm.DetailItemId.ToString()] }); } } if (productDetail != null) { _productDetailService.Add(productDetail); } _productService.Edit(products); ViewBag.tag = tags; return(RedirectToAction("/edit/" + products.ProductId)); } ViewBag.ProductGroupId = new SelectList(_productGroupService.ProductGroups(), "ProductGroupId", "GroupTitle", products.ProductGroupId); return(View(products)); }