public ActionResult Create(EditPostModel model) { if (ModelState.IsValid) { var postToSave = model.EditPost; var idsToAdd = (from tagModel in model.CurrentTags where tagModel.Checked select tagModel.Id).ToList(); if (_repo.UpdatePost(postToSave, idsToAdd, model.ChosenCategory)) { TempData["message"] = string.Format("{0} успешно добавлен", postToSave.Title); return RedirectToAction("Index"); } return View("Edit", model); } return View("Edit", model); }
public ViewResult Create() { var tagsForCheckBox = _repo.Tags().Select(tag => new TagsCheckBoxModel { Checked = false, Id = tag.Id, Name = tag.Name }).ToList(); var newPostModel = new EditPostModel { EditPost = new Post { Title = "Новый пост", PostedOn = DateTime.UtcNow }, AllCategories = _repo.AllCategoriesToList(), CurrentTags = tagsForCheckBox }; return View("Edit", newPostModel); }
public ActionResult Edit(EditPostModel model) { if (ModelState.IsValid) { var postToSave = model.EditPost; List<Category> cats = _repo.Categories(); List<Tag> tags = _repo.Tags(); postToSave.Category = cats.Find(m => m.Name.Equals(model.ChosenCategory)); List<Tag> tagsToAdd = (from tagModel in model.CurrentTags where tagModel.Checked select tags.Find(x => x.Id == tagModel.Id)).ToList(); postToSave.Tags = tagsToAdd; List<int> idsToAdd = tagsToAdd.Select(item => item.Id).ToList(); if (_repo.UpdatePost(postToSave, idsToAdd, string.Empty)) { TempData["message"] = string.Format("{0} успешно сохранен", postToSave.Title); return RedirectToAction("Index"); } return View(model); } return View(model); }
public ViewResult Edit(int postId) { var editPost = _repo.GetPost(postId); var tagsForCheckBox = new List<TagsCheckBoxModel>(); var postTags = editPost.Tags.Select(t => t.Name).ToList(); foreach (var tag in _repo.Tags()) { if (postTags.Contains(tag.Name)) { tagsForCheckBox.Add(new TagsCheckBoxModel { Checked = true, Id = tag.Id, Name = tag.Name }); } else { tagsForCheckBox.Add(new TagsCheckBoxModel { Checked = false, Id = tag.Id, Name = tag.Name }); } } var editPostModel = new EditPostModel { EditPost = editPost, ChosenCategory = editPost.Category.Name, AllCategories = _repo.AllCategoriesToList(), CurrentTags = tagsForCheckBox }; return View(editPostModel); }