public static KeywordViewModel CreateFromKeyword(Keyword k) { KeywordViewModel keywordViewModel = new KeywordViewModel { Keyword = k.KeywordText, }; return keywordViewModel; }
public ActionResult Edit(Keyword model) { ViewBag.KeywordAdminActive = true; if (ModelState.IsValid) { if (model.KeywordText != null) { if (model.KeywordText.Trim() != "") { Keyword oldKeyword = db.Keywords.Find(model.ID); String modelKeywordTextLowerCase = model.KeywordText.ToLower(); Keyword versionOfNewKeywordInDB = findKeywordFromText(model.KeywordText); if (oldKeyword.KeywordText.ToLower().Equals(modelKeywordTextLowerCase)) { oldKeyword.KeywordText = model.KeywordText; } else { if (versionOfNewKeywordInDB != null) { removeOldKeywordFromQuestions(versionOfNewKeywordInDB, oldKeyword); db.Keywords.Remove(oldKeyword); } else { oldKeyword.KeywordText = model.KeywordText; } } db.SaveChanges(); RouteValueDictionary routeValueDictionary = new RouteValueDictionary(); routeValueDictionary.Add("page", 1); return RedirectToAction("Index", "KeywordsAdmin", routeValueDictionary); } else { ModelState.AddModelError("", "Keyword needs characters other than space(s)."); return View(model); } } else { ModelState.AddModelError("", "Keyword cannot be empty."); return View(model); } } else { ModelState.AddModelError(model.KeywordText, "Keyword cannot be edited."); return View(model); } }
/// <summary> Helper method to remove all of the references to the old keyword and change it to all the references to the new keyword </summary> /// <param name="newKeyword"> The new keyword that we want the references to point to </param> /// <param name="oldKeyword"> The old keyword that we want to remove all references to </param> /// <returns> None </returns> private void removeOldKeywordFromQuestions(Keyword newKeyword, Keyword oldKeyword) { DbSet<Question> allQuestions = db.Questions; String newKeywordTestLowerCase = newKeyword.KeywordText.ToLower(); foreach (Question q in allQuestions) { List<Keyword> keywordsInQuestion = q.Keywords; foreach (Keyword k in keywordsInQuestion) { String lowerCaseCurKeyword = k.KeywordText.ToLower(); if (k.KeywordText.Equals(newKeywordTestLowerCase)) { q.Keywords.Remove(oldKeyword); q.Keywords.Add(newKeyword); break; } } } }