// GET: SharedItems public async Task <IActionResult> Index(string searchString, int?categoryID) { ViewData["CurrentFilter"] = searchString; ViewData["categoryID"] = categoryID ?? 0; var pvm = new SharedItemViewModel { Categories = await _context.Categories.Include(c => c.SharedItems).AsNoTracking().ToListAsync() }; // All the categories if (categoryID == null || categoryID <= 0) { pvm.SharedItems = await _context.SharedItems.Include(p => p.Category).OrderByDescending(p => p.PostTime).ToListAsync(); } else { pvm.SharedItems = await _context.SharedItems.Where(c => c.Category.CategoryID == categoryID) .Include(p => p.Category).OrderByDescending(p => p.PostTime).ToListAsync(); } // searching condition if (!string.IsNullOrEmpty(searchString)) { pvm.SharedItems = pvm.SharedItems.Where(p => p.Name.ToLower().Contains(searchString.ToLower())).OrderByDescending(p => p.PostTime).ToList(); } return(View(pvm)); }
public async Task <IActionResult> Index() { var pvm = new SharedItemViewModel(); pvm.Categories = await _context.Categories .AsNoTracking() .ToListAsync(); pvm.SharedItems = await _context.SharedItems.Include(p => p.Category).ToListAsync(); return(View(pvm)); }