// GET: Monster public async Task <IActionResult> Index(string monsterType, string searchString) { // use LINQ to get list of types. retrieve all data IQueryable <string> typeQuery = from m in _context.Monsters orderby m.Type select m.Type; // LINQ query to select monster var monsters = from m in _context.Monsters select m; if (!String.IsNullOrEmpty(searchString)) { monsters = monsters.Where(s => s.Name.Contains(searchString)); } if (!String.IsNullOrEmpty(monsterType)) { monsters = monsters.Where(x => x.Type == monsterType); } var monsterTypeVM = new MonsterTypeViewModel(); monsterTypeVM.Types = new SelectList(await typeQuery.Distinct().ToListAsync()); monsterTypeVM.Monsters = await monsters.ToListAsync(); monsterTypeVM.SearchString = searchString; return(View(monsterTypeVM)); }
// GET: Monsters public async Task <IActionResult> Index(string monsterType, string searchString) { IQueryable <string> typeQuery = from m in _context.Monster orderby m.Type select m.Type; var monsters = from m in _context.Monster select m; if (!String.IsNullOrEmpty(searchString)) { monsters = monsters.Where(s => s.Called.Contains(searchString)); } if (!string.IsNullOrEmpty(monsterType)) { monsters = monsters.Where(x => x.Type == monsterType); } var monsterTypeVM = new MonsterTypeViewModel { Types = new SelectList(await typeQuery.Distinct().ToListAsync()), Monsters = await monsters.ToListAsync() }; return(View(monsterTypeVM)); }