// GET: Books public ActionResult Index(string searchString, int page = 1) { try { var books = db.Books.AsQueryable(); int pageSize = 10; if (!string.IsNullOrEmpty(searchString)) { books = (db.Books.Include(b => b.Author).Include(b => b.CountryPublished).Where(n => n.Title.Contains(searchString) || n.Author.FullName.Contains(searchString))); if (!books.Any()) { return(PartialView("SearchViewNotFound", searchString)); } } BooksListModel model = new BooksListModel { BooksList = BookRelase.GetBookResult(books) }; if (page > model.BooksList.ToPagedList(page, pageSize).PageCount) { return(RedirectToAction("Index")); } return(Request.IsAjaxRequest() ? (ActionResult)PartialView("IndexPartial", model.BooksList.ToPagedList(page, pageSize)) : View(model.BooksList.ToPagedList(page, pageSize))); } catch { return(RedirectToAction("Index")); } }
// GET: Admin public ActionResult Index(string searchString, string sortOption, int page = 1) { try { int pageSize = 5; var books = db.Books.ToList(); if (!string.IsNullOrEmpty(searchString)) { books = (db.Books.Include(b => b.Author).Include(b => b.CountryPublished).Where(n => n.Title.Contains(searchString) || n.Author.FullName.Contains(searchString))).ToList(); if (!books.Any()) { //return Content("This book is not found <a href='~Admin/Index'>Go</a> "); return(PartialView("BookNot", searchString)); } } BooksListModel model = new BooksListModel { BooksList = BookRelase.GetBookResult(books) }; switch (sortOption) { case "Title_ASC": model.BooksList = model.BooksList.OrderBy(n => n.Title).ToList(); break; case "Title_DESC": model.BooksList = model.BooksList.OrderByDescending(n => n.Title).ToList(); break; case "Price_ASC": model.BooksList = model.BooksList.OrderBy(n => n.totalPrice).ToList(); break; case "Price_DESC": model.BooksList = model.BooksList.OrderByDescending(n => n.totalPrice).ToList(); break; case "Author_ASC": model.BooksList = model.BooksList.OrderBy(n => n.Author.FullName).ToList(); break; case "Author_DESC": model.BooksList = model.BooksList.OrderByDescending(n => n.Author.FullName).ToList(); break; case "PageCount_ASC": model.BooksList = model.BooksList.OrderBy(n => n.PagesCount).ToList(); break; case "PageCount_DESC": model.BooksList = model.BooksList.OrderByDescending(n => n.PagesCount).ToList(); break; case "Country_ASC": model.BooksList = model.BooksList.OrderBy(n => n.CountryPublished.CountryName).ToList(); break; case "Country_DESC": model.BooksList = model.BooksList.OrderByDescending(n => n.CountryPublished.CountryName).ToList(); break; default: model.BooksList = model.BooksList.OrderBy(n => n.Id).ToList(); break; } if (page > model.BooksList.ToPagedList(page, pageSize).PageCount) { return(RedirectToAction("Index")); } return(Request.IsAjaxRequest() ? (ActionResult)PartialView("IndexPartial", model.BooksList.ToPagedList(page, pageSize)) : View(model.BooksList.ToPagedList(page, pageSize))); } catch { return(RedirectToAction("Index")); } }
public ActionResult Index() { var books = db.Books.ToList(); BooksListModel model = new BooksListModel { BooksList = BookRelase.GetBookResult(books) }; Random rnd = new Random(); return(View(model.BooksList.OrderBy(n => rnd.Next()).Take(5))); }