// GET: Stories/Edit/5 public ActionResult Edit(short?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Story story = db.Stories.Find(id); if (story == null) { return(HttpNotFound()); } ViewBag.IsAboutId = new SelectList(db.Infos, "InfoId", "Name", story.IsAboutId); StoryEditVM model = new StoryEditVM(story); List <short> selectedTags = db.StoryTags.Where(t => t.StoryId == id).Select(t => t.TagId).ToList(); ViewBag.Selected = selectedTags; ViewBag.Tags = db.Tags.ToList(); return(View(model)); }
public ActionResult Edit([Bind(Include = "StoryId,IsAboutId,DateTold,CommissionedBy,IsCannon,TheContent,Title")] StoryEditVM story, List <short> tags) { if (ModelState.IsValid) { #region Update Story Story daStory = new Story { StoryId = story.StoryId, DateTold = story.DateTold, CommissionedBy = story.CommissionedBy, IsAboutId = story.IsAboutId, IsCannon = story.IsCannon, TheContent = story.TheContent, Title = story.Title }; db.Entry(daStory).State = EntityState.Modified; db.SaveChanges(); #endregion #region Update Tags List <short> currentTagIds = db.StoryTags.Where(x => x.StoryId == story.StoryId).Select(x => x.StoryId).ToList(); if (tags != null) { foreach (short t in tags) { //if this is an already existing tag if (currentTagIds.Contains(t)) { currentTagIds.Remove(t); } else { StoryTag newTag = new StoryTag { StoryId = story.StoryId, TagId = t }; db.StoryTags.Add(newTag); } } } if (currentTagIds.Count != 0) { foreach (short id in currentTagIds) { StoryTag gone = db.StoryTags.Where(x => x.StoryId == story.StoryId & x.TagId == id).FirstOrDefault(); db.StoryTags.Remove(gone); } } db.SaveChanges(); #endregion return(RedirectToAction("Details", "Infos", new { id = story.IsAboutId })); } ViewBag.IsAboutId = new SelectList(db.Infos, "InfoId", "Name", story.IsAboutId); ViewBag.Tags = db.Tags.ToList(); if (tags != null) { ViewBag.Selected = tags; } else { ViewBag.Selected = new List <short>(); } return(View(story)); }