BookManagement.Web.Controllers.HomeController.Index C# (CSharp) Method

Index() public method

public Index ( int categoryId, int page ) : System.Web.Mvc.ActionResult
categoryId int
page int
return System.Web.Mvc.ActionResult
        public ActionResult Index(int? categoryId, int? page)
        {
            categoryId = (categoryId == null) ? 0 : categoryId.Value;

            int pageNumber = (page ?? 1);

            // Get all book
            if(categoryId == 0)
            {
                ViewBag.Category = "All";

                var listBook = (from b in db.Books select b).OrderByDescending(book => book.Id);
                return View(listBook.ToPagedList(pageNumber, pageSize));
            }

            // Get book by id category
            Category ca = db.Categories.Where(c => c.Id == categoryId).ToList()[0];
            ViewBag.Category = ca.Name;

            var list = db.Books.Where(b => b.Category.Id == categoryId).OrderByDescending(book => book.Id);
            return View(list.ToPagedList(pageNumber, pageSize));
        }