// GET: Edit public ActionResult Edit(int?wordId, int?id) { if (wordId is null || id is null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } var service = CreateSentenceService((int)wordId); var sentence = service.GetSentenceById((int)id); if (sentence is null) { return(HttpNotFound()); } var model = new SentenceEdit { SentenceId = (int)id, SentenceContent = sentence.SentenceContent, SentenceTranslation = sentence.SentenceTranslation }; ViewData["WordId"] = (int)wordId; return(View(model)); }
// Update public bool UpdateSentence(SentenceEdit model) { var entity = _context .Sentences .SingleOrDefault(e => e.UserId == _userId && e.SentenceId == model.SentenceId); entity.SentenceContent = model.SentenceContent; entity.SentenceTranslation = model.SentenceTranslation; return(_context.SaveChanges() == 1); }
public ActionResult Edit(int languageId, int wordId, int id, SentenceEdit model) { if (!ModelState.IsValid) { return(View(model)); } if (model.SentenceId != id) { ModelState.AddModelError("", "Id Mismatch"); return(View(model)); } var service = CreateSentenceService(wordId); if (service.UpdateSentence(model)) { TempData["SaveResult"] = "Sentence was successfully updated."; return(RedirectToAction("Details", "Word", new { languageId, id = wordId })); } ModelState.AddModelError("", "Sentence was not updated."); return(View(model)); }