public ActionResult Edit(PostEditViewModels model) { if (ModelState.IsValid) { Post post = db.Posts.Find(model.Id); post.Content = model.Content; post.CategoryId = model.CategoryId; post.Title = model.Title; post.Slug = model.Slug; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.CategoryId = new SelectList(db.Categories.ToList(), "Id", "CategoryName"); return(View()); }
public ActionResult Edit(int id) { ViewBag.CategoryId = new SelectList(db.Categories.ToList(), "Id", "CategoryName"); PostEditViewModels vm = db.Posts.Select(x => new PostEditViewModels { Id = x.Id, CategoryId = x.CategoryId, Content = x.Content, Title = x.Title, Slug = x.Slug }).FirstOrDefault(x => x.Id == id); return(View(vm)); }
public ActionResult New(PostEditViewModels model) { if (ModelState.IsValid) { Post post = new Post { Title = model.Title, Content = model.Content, CategoryId = model.CategoryId, AuthorId = User.Identity.GetUserId(), CreateTime = DateTime.Now, Slug = model.Slug }; db.Posts.Add(post); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.CategoryId = new SelectList(db.Categories.ToList(), "Id", "CategoryName"); return(View("Edit", new PostEditViewModels())); }