public ActionResult KeywordTagSelection(FormCollection form, KeywordTagSelectionViewModel viewModel) { //... // Restore tag list viewModel.Tags = _context.Tags.Where(p => p.TagParentId != null).ToList(); if (!ModelState.IsValid) { ViewBag.Result = false; ViewBag.ResultMessage = "Lütfen belirtilen alanları doldurunuz!"; return(PartialView(viewModel)); } try { //... // Delete previously added tags var previousSelectedTags = _context.KeywordTags.Where(p => p.KeywordId == viewModel.KeywordId).ToList(); foreach (var previousSelectedTag in previousSelectedTags) { _context.KeywordTags.Remove(previousSelectedTag); _context.SaveChanges(); } //... // Create Tags for newly created keyword foreach (var item in form["SelectedTags"].Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)) { var keywordTag = new KeywordTag { KeywordId = viewModel.KeywordId, TagId = Convert.ToInt32(item), CreateDate = DateTime.Now }; _context.KeywordTags.Add(keywordTag); _context.SaveChanges(); } ViewBag.Result = true; ViewBag.ResultMessage = "Girdiğiniz içerik başarıyla kaydedilmiştir."; return(PartialView(viewModel)); } catch (Exception exception) { throw new Exception("Error in ManagementController.CreateKeywordTagSelection [Post]", exception); } }
public ActionResult KeywordTagSelection(int keywordId = 0) { try { var keywordTagSelectionListViewModel = new KeywordTagSelectionViewModel { KeywordId = keywordId, Tags = _context.Tags.Where(p => p.TagParentId != null).ToList(), SelectedTags = _context.KeywordTags.Where(p => p.KeywordId == keywordId).Select(p => p.TagId).ToArray() }; return(View(keywordTagSelectionListViewModel)); } catch (Exception exception) { throw new Exception("Error in ManagementController.CreateKeywordTagSelection [Get]", exception); } }