public class BookController : Controller { private readonly BookRepository _bookRepository = null; public BookController(BookRepository bookRepository) { _bookRepository = bookRepository; } public ViewResult AddNewBook(bool isSuccess=false, int bookId=0) { ViewBag.isSuccess = isSuccess; ViewBag.BookId = bookId; return View(); } [HttpPost] public IActionResult AddNewBook(BookModel bookModel) { int id = _bookRepository.AddNewBook(bookModel); if (id > 0) { //return RedirectToAction("AddNewBook"); or return RedirectToAction(nameof(AddNewBook), new { isSuccess = true , bookId = id}); } return View(); } } --------------------------------------------------------------------------------------- public class BookRepository { private readonly BookStoreContext _context = null; public BookRepository(BookStoreContext context) { _context = context; } public int AddNewBook(BookModel model) { var newBook = new Books() { Author = model.Author, CreatedOn = DateTime.UtcNow, Description = model.Description, Title = model.Title, TotalPages = model.TotalPages, UpdatedOn = DateTime.UtcNow }; _context.Books.Add(newBook); _context.SaveChanges(); return newBook.Id; } -------------------------------------------------------------------------------- public class BookStoreContext : DbContext { public BookStoreContext(DbContextOptions options):base(options) { } public DbSet Books { get; set; } } ---------------------------------------------------------------------------- } public class BookModel { public int Id { get; set; } public string Title { get; set; } public string Author { get; set; } public string Description { get; set; } public string Category { get; set; } public string Language { get; set; } public int TotalPages { get; set; } } --------------------------------------------------------------------------- public class Books { public int Id { get; set; } public string Title { get; set; } public string Author { get; set; } public string Description { get; set; } public string Category { get; set; } public string Language { get; set; } public int TotalPages { get; set; } public DateTime? CreatedOn { get; set; } public DateTime? UpdatedOn { get; set; } }