public JsonResult Update(int keywordID, string products) { var jsonSuccessResult = new JsonSuccessResult(); try { string[] arrProducts = products.Split(','); var curList = ProductKeywords.GetByKeywordID(keywordID); foreach (var product in arrProducts) { int productID = Int32.Parse(product); if (!curList.Any(item => item.ProductID == productID && item.KeywordID == keywordID)) { var kp = new ProductKeyword { ProductID = productID, KeywordID = keywordID, LastUpdate = DateTime.Now }; ProductKeywords.Insert(kp); } else { curList.Remove(curList.Single(cls => cls.KeywordID == keywordID && cls.ProductID == productID)); } } foreach (var item in curList) { ProductKeywords.Delete(item.ID); } jsonSuccessResult.Success = true; } catch (Exception ex) { jsonSuccessResult.Errors = new string[] { ex.Message }; jsonSuccessResult.Success = false; } return(new JsonResult() { Data = jsonSuccessResult }); }
private static void SaveKeywords(EditProduct editProduct, int productID) { var curList = ProductKeywords.GetByProductID(productID); foreach (var key in editProduct.Keywords) { if (key.KeywordID == -1) { key.KeywordID = InsertKeyword(key.Title); } if (!curList.Any(item => item.ID == key.ID)) { var rpt = IsRepeatKey(key.KeywordID, productID); if (!rpt) { var productKeyword = Mapper.Map <ProductKeyword>(key); productKeyword.ProductID = productID; ProductKeywords.Insert(productKeyword); } } else { ProductKeywords.Update(key); curList.Remove(curList.Single(cls => cls.ID == key.ID)); } } foreach (var item in curList) { ProductKeywords.Delete(item.ID); } }