public IActionResult Index() { var model = new SuppliersCategoriesBooksViewModel() { Suppliers = _context.Suppliers, Categories = _context.Categories, Books = _context.Books.Include(b => b.Category).Include(b => b.Supplier) }; return(View(model)); //var bookStoreContext = _context.Books.Include(b => b.Category).Include(b => b.Supplier); //return View(await bookStoreContext.ToListAsync()); }
public async Task <IActionResult> Index() { IQueryable <string> categoryQuery = from c in _context.Categories select c.Name; IQueryable <string> supplierQuery = from s in _context.Suppliers select s.Name; var books = from s in _context.Books select s; var model = new SuppliersCategoriesBooksViewModel() { CategoriesList = new SelectList(await categoryQuery.Distinct().ToListAsync()), SupplierList = new SelectList(await supplierQuery.Distinct().ToListAsync()), Books = await books.Take(8).ToListAsync(), }; return(View(model)); //var bookStoreContext = _context.Books.Include(b => b.Category).Include(b => b.Supplier); //return View(await bookStoreContext.ToListAsync()); }
public async Task <IActionResult> Book(string searchString, string category, string supplier, int?pageNumber, string currentFilter) { ViewData["Category"] = category; ViewData["Supplier"] = supplier; if (searchString != null) { pageNumber = 1; } else { searchString = currentFilter; } ViewData["CurrentFilter"] = searchString; var books = from s in _context.Books select s; IQueryable <string> categoryQuery = from c in _context.Categories select c.Name; IQueryable <string> supplierQuery = from s in _context.Suppliers select s.Name; if (!String.IsNullOrEmpty(searchString) && !String.IsNullOrEmpty(category) && !String.IsNullOrEmpty(supplier)) { books = books.Where(s => s.Name.Contains(searchString) && s.Category.Name.Contains(category) && s.Supplier.Name.Contains(supplier)); } else if (!String.IsNullOrEmpty(searchString) && !String.IsNullOrEmpty(category)) { books = books.Where(s => s.Name.Contains(searchString) && s.Category.Name.Contains(category)); } else if (!String.IsNullOrEmpty(category) && !String.IsNullOrEmpty(supplier)) { books = books.Where(s => s.Category.Name.Contains(category) && s.Supplier.Name.Contains(supplier)); } else if (!String.IsNullOrEmpty(searchString) && !String.IsNullOrEmpty(supplier)) { books = books.Where(s => s.Name.Contains(searchString) && s.Supplier.Name.Contains(supplier)); } else if (!String.IsNullOrEmpty(searchString)) { books = books.Where(s => s.Name.Contains(searchString)); } else if (!String.IsNullOrEmpty(category)) { books = books.Where(s => s.Category.Name.Contains(category)); } else if (!String.IsNullOrEmpty(supplier)) { books = books.Where(s => s.Supplier.Name.Contains(supplier)); } int pageSize = 4; int totalPages = (int)Math.Ceiling(books.Count() / (double)pageSize); var model = new SuppliersCategoriesBooksViewModel() { Suppliers = await _context.Suppliers.ToListAsync(), Categories = await _context.Categories.ToListAsync(), CategoriesList = new SelectList(await categoryQuery.Distinct().ToListAsync()), SupplierList = new SelectList(await supplierQuery.Distinct().ToListAsync()), Books = await PaginatedList <Book> .CreateAsync(books.AsNoTracking(), pageNumber ?? 1, pageSize), PageIndex = pageNumber ?? 1, TotalPages = totalPages, }; return(View(model)); }