2.4 KiB
Now when you submit a search, the URL contains the search query string. Searching will also go to the HttpGet Index
action method, even if you have a HttpPost Index
method.
The following markup shows the change to the form
tag:
<form asp-controller="Movies" asp-action="Index" method="get">
Adding Search by Genre
Add the following MovieGenreViewModel
class to the Models folder:
[!code-csharpMain]
The movie-genre view model will contain:
- A list of movies.
- A
SelectList
containing the list of genres. This will allow the user to select a genre from the list. movieGenre
, which contains the selected genre.
Replace the Index
method in MoviesController.cs
with the following code:
[!code-csharpMain]
The following code is a LINQ
query that retrieves all the genres from the database.
[!code-csharpMain]
The SelectList
of genres is created by projecting the distinct genres (we don't want our select list to have duplicate genres).
movieGenreVM.genres = new SelectList(await genreQuery.Distinct().ToListAsync())
Adding search by genre to the Index view
Update Index.cshtml
as follows:
[!code-HTMLMain]
Test the app by searching by genre, by movie title, and by both.