public async Task <IActionResult> Edit(int id, [Bind("AmazonKeywordID,Keyword")] AmazonKeyword amazonKeyword)
        {
            if (id != amazonKeyword.AmazonKeywordID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(amazonKeyword);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!AmazonKeywordExists(amazonKeyword.AmazonKeywordID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction("Index"));
            }
            return(View(amazonKeyword));
        }
        public async Task <IActionResult> Create([Bind("AmazonKeywordID,Keyword")] AmazonKeyword amazonKeyword)
        {
            if (ModelState.IsValid)
            {
                _context.Add(amazonKeyword);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(amazonKeyword));
        }
Example #3
0
        private void UpdateProductKeywords(IList <AssignedKeywordsViewModel> selectedKeywords, AmazonProduct productToUpdate)
        {
            var keywordsFromDb = _context.AmazonKeywords
                                 .ToDictionary(c => c.Keyword,
                                               c => c.AmazonKeywordID);

            for (int i = 0; i < selectedKeywords.Count; i++)
            {
                KeywordAssignment keywordToRemove = productToUpdate.Keywords.SingleOrDefault(a => a.Order == i);
                if (keywordToRemove != null)
                {
                    _context.Remove(keywordToRemove);
                    _context.SaveChanges();
                }

                if (selectedKeywords[i].Keyword != null)
                {
                    if (keywordsFromDb.Keys.Contains(selectedKeywords[i].Keyword))
                    {
                        productToUpdate.Keywords.Add(new KeywordAssignment {
                            ProductID = productToUpdate.AmazonProductID, KeywordID = keywordsFromDb[selectedKeywords[i].Keyword], Order = i
                        });
                    }
                    else
                    {
                        AmazonKeyword newKeyword = new AmazonKeyword {
                            Keyword = selectedKeywords[i].Keyword
                        };
                        _context.AmazonKeywords.Add(newKeyword);
                        _context.SaveChanges();
                        productToUpdate.Keywords.Add(new KeywordAssignment {
                            ProductID = productToUpdate.AmazonProductID, KeywordID = newKeyword.AmazonKeywordID, Order = i
                        });
                        keywordsFromDb[newKeyword.Keyword] = newKeyword.AmazonKeywordID;
                    }
                }
            }
        }