public ActionResult New() { var model = new PostsFormViewModel() { Categories = db.Categories.ToList() }; return(View("PostsForm", model)); }
public ViewResult New() { var categories = _context.Categories.ToList(); //var types = _context.EventTypes.ToList(); var viewModel = new PostsFormViewModel { Categories = categories }; return(View("PostForm", viewModel)); }
public ActionResult Edit(int id) { var posts = _context.Posts.SingleOrDefault(c => c.Id == id); if (posts == null) { return(HttpNotFound()); } var viewModel = new PostsFormViewModel(posts) { //Movie = movie, Categories = _context.Categories.ToList() }; return(View("PostForm", viewModel)); }
public ActionResult Save(Post posts) { if (!ModelState.IsValid) { var viewModel = new PostsFormViewModel { Categories = _context.Categories.ToList() }; return(View("PostForm", viewModel)); } if (posts.Id == 0) { string currentUserId = User.Identity.GetUserId(); ApplicationUser currentUser = _context.Users.FirstOrDefault(x => x.Id == currentUserId); posts.DateAdded = DateTime.Now; posts.OwnerId = currentUserId; //User.Identity.GetUserId(); _context.Posts.Add(posts); } else { // add stuff var postInDb = _context.Posts.Single(m => m.Id == posts.Id); postInDb.CategoryId = posts.CategoryId; // doesnt apply for edit movieInDb.DateAdded = movie.DateAdded; postInDb.Name = posts.Name; } try { _context.SaveChanges(); } catch (DbEntityValidationException e) { Console.WriteLine(e); } return(RedirectToAction("Index", "Post")); }