public async Task <IActionResult> Create(BookAndReviewsViewModel model) { if (ModelState.IsValid) { model.NewReviewObj.BookId = model.bookId; model.NewReviewObj.DateAdded = DateTime.Now; _db.Add(model.NewReviewObj); await _db.SaveChangesAsync(); return(RedirectToAction(nameof(Create), new { bookId = model.bookId })); } var book = _db.Books.FirstOrDefault(c => c.Id == model.bookId); var newModel = new BookAndReviewsViewModel { bookId = book.Id, Author = book.Author, Title = book.Title, Genre = book.Genre, ISBN = book.ISBN, Year = book.Year, UserId = book.UserId, ReviewTypesObj = _db.ReviewTypes.ToList(), PastReviewsObj = _db.Reviews.Where(s => s.BookId == model.bookId).OrderByDescending(s => s.DateAdded).Take(5) }; return(View(newModel)); }
//GET : Reviews/Create public IActionResult Create(int bookId) { var book = _db.Books.FirstOrDefault(c => c.Id == bookId); var model = new BookAndReviewsViewModel { bookId = book.Id, Author = book.Author, Title = book.Title, Genre = book.Genre, ISBN = book.ISBN, Year = book.Year, UserId = book.UserId, ReviewTypesObj = _db.ReviewTypes.ToList(), PastReviewsObj = _db.Reviews.Where(s => s.BookId == bookId).OrderByDescending(s => s.DateAdded).Take(5) }; return(View(model)); }