public ActionResult Create(PostsIndexViewModel vm) { if (ModelState.IsValid) { vm.NewPost.CreatedDate = DateTime.Now; vm.NewPost.ModifiedDate = DateTime.Now; vm.NewPost.User = db.Users.Find(User.Identity.GetUserId()); vm.NewPost.User.Posts.Add(vm.NewPost); vm.NewPost.Thread = db.Threads.FirstOrDefault(t => t.DateArchived == null); if (vm.NewPost.Thread == null) vm.NewPost.Thread = new Thread() { DateArchived = null }; db.SaveChanges(); return Redirect(String.Format("{0}#post{1}", Url.Action("Index"), vm.NewPost.Id)); } return View("Index", vm.NewPost); }
public ActionResult Index(int? id, int? page) { var vm = new PostsIndexViewModel() { ExistingPosts = new List<Post>(), NewPost = new Post(), Thread = new Thread() }; if (id.HasValue) { var specificThread = db.Threads.Find(id.Value); if (specificThread == null) return HttpNotFound(); vm.ExistingPosts = specificThread.Posts; vm.Thread = specificThread; } else { var defaultThread = db.Threads.FirstOrDefault(t => t.DateArchived == null); if (defaultThread != null) { vm.ExistingPosts = defaultThread.Posts; vm.Thread = defaultThread; } } // Do pagination int lastPageNumber = ((vm.ExistingPosts.Count() - 1) / PAGESIZE) + 1; vm.ExistingPosts = vm.ExistingPosts.OrderBy(p => p.CreatedDate).ToPagedList(page.HasValue ? page.Value : lastPageNumber, PAGESIZE); // Last seen UpdateUserLastSeen(); return View(vm); }