public ActionResult Add() { Post post = new Post(); ViewBag.ActionName = "Пост - Добавление"; ViewBag.Tags = _tagRepository.Tags.ToList(); return View("Edit", post); }
public void Save(Post post, int[] selectedTags) { if (post.Id == 0) { if (selectedTags != null) { foreach (var tag in _db.Tags.Where(t => selectedTags.Contains(t.Id))) { post.Tags.Add(tag); } } post.CreateDate = DateTime.Now; _db.Posts.Add(post); } else { Post dbEntry = _db.Posts.Find(post.Id); if (dbEntry != null) { dbEntry.Title = post.Title; dbEntry.IntroText = post.IntroText; dbEntry.MainText = post.MainText; dbEntry.IsPublished = post.IsPublished; if (selectedTags != null) { dbEntry.Tags.Clear(); foreach (var tag in _db.Tags.Where(t => selectedTags.Contains(t.Id))) { dbEntry.Tags.Add(tag); } } _db.Entry(dbEntry).State = EntityState.Modified; } } _db.SaveChanges(); }
public ActionResult Save(Post post, int[] selectedTags) { if (ModelState.IsValid) { _postRepository.Save(post, selectedTags); return RedirectToAction("List", "Posts", new { page = TempData["page"], order = TempData["order"], published = TempData["published"] }); } ViewBag.Tags = _tagRepository.Tags.ToList(); return View(post); }