So, here's the craic: I'm building a site similar to Gumtree where users can post BluRay discs for sale. I need users to be able to search for items by a number of fields, one of the being 'Category'.
I've set up a search function which results in a view being populated with the relevant data but currently the search bar is a free text input - I want it to be a drop down list populated with the various categories that have been input thus far (and hence be dynamic, showing new categories in the list as and when new ones are created).
I have a method in the controller which allows me to find items relevant to the search input:
// GET: Movies
public async Task<IActionResult> MoviesByCategory(string searchString)
{
//ViewData["currentSearchKey"] = searchString;
if (!String.IsNullOrEmpty(searchString))
{
var movies = (from m in _context.Movies.Include(m => m.MovieCategory).Include(m => m.MovieCondition).Include(m => m.MovieLocation).Include(m => m.MovieSaleStatus)
join c in _context.MovieCategories on m.MovieCategoryID equals c.MovieCategoryID
where(c.Category.Contains(searchString))
select m).ToListAsync();
return View("Index", await movies);
}
var applicationDbContext = _context.Movies.Include(m => m.MovieCategory).Include(m => m.MovieCondition).Include(m => m.MovieLocation).Include(m => m.MovieSaleStatus);
return View("Index",await applicationDbContext.ToListAsync());
}
And I also have a search input box on the corresponding view which enables the search:
Search By Category: <input type="text" name="searchString" />
<input type="submit" name="Search" value="Find" class="btn btn-success" />
How do I go about changing the search input on the view to a drop down populated with the data from the current categories?
Many thanks.