2017-04-10 08:52:07 +08:00
<!--
2018-05-31 14:01:13 +08:00
[!code-html[ ](~/tutorials/first-mvc-app/start-mvc/sample/MvcMovie/Views/Shared/_Layout.cshtml?highlight=7,31 )]
2017-04-10 08:52:07 +08:00
2018-05-31 14:01:13 +08:00
[!code-csharp[ ](~/tutorials/first-mvc-app/start-mvc/sample/MvcMovie/Controllers/MoviesController.cs?name=snippet_1stSearch )]
2017-04-10 08:52:07 +08:00
2018-05-31 14:01:13 +08:00
[!code-csharp[ ](~/tutorials/first-mvc-app/start-mvc/sample/MvcMovie/Controllers/MoviesController.cs?name=snippet_SearchNull )]
2017-04-10 08:52:07 +08:00
2018-05-31 14:01:13 +08:00
![Index view ](~/tutorials/first-mvc-app/search/_static/ghost.png )
2017-04-10 08:52:07 +08:00
2018-05-31 14:01:13 +08:00
[!code-csharp[ ](~/tutorials/first-mvc-app/start-mvc/sample/MvcMovie/Startup.cs?highlight=5&name=snippet_1 )]
2017-04-10 08:52:07 +08:00
-->
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.
2018-05-31 14:01:13 +08:00
![Browser window showing the searchString=ghost in the Url and the movies returned, Ghostbusters and Ghostbusters 2, contain the word ghost ](~/tutorials/first-mvc-app/search/_static/search_get.png )
2017-04-10 08:52:07 +08:00
The following markup shows the change to the `form` tag:
```html
< form asp-controller = "Movies" asp-action = "Index" method = "get" >
```
2017-06-22 04:52:49 +08:00
## Adding Search by genre
2017-04-10 08:52:07 +08:00
Add the following `MovieGenreViewModel` class to the *Models* folder:
2018-05-31 14:01:13 +08:00
[!code-csharp[ ](~/tutorials/first-mvc-app/start-mvc/sample/MvcMovie/Models/MovieGenreViewModel.cs )]
2017-04-10 08:52:07 +08:00
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.
2018-10-16 03:16:49 +08:00
* `MovieGenre` , which contains the selected genre.
2017-04-10 08:52:07 +08:00
Replace the `Index` method in `MoviesController.cs` with the following code:
2018-05-31 14:01:13 +08:00
[!code-csharp[ ](~/tutorials/first-mvc-app/start-mvc/sample/MvcMovie/Controllers/MoviesController.cs?name=snippet_SearchGenre )]
2017-04-10 08:52:07 +08:00
The following code is a `LINQ` query that retrieves all the genres from the database.
2018-05-31 14:01:13 +08:00
[!code-csharp[ ](~/tutorials/first-mvc-app/start-mvc/sample/MvcMovie/Controllers/MoviesController.cs?name=snippet_LINQ )]
2017-04-10 08:52:07 +08:00
The `SelectList` of genres is created by projecting the distinct genres (we don't want our select list to have duplicate genres).
```csharp
movieGenreVM.genres = new SelectList(await genreQuery.Distinct().ToListAsync())
2018-10-16 03:16:49 +08:00
```
2017-04-10 08:52:07 +08:00
## Adding search by genre to the Index view
Update `Index.cshtml` as follows:
2018-05-31 14:01:13 +08:00
[!code-HTML[ ](~/tutorials/first-mvc-app/start-mvc/sample/MvcMovie/Views/Movies/IndexFormGenreNoRating.cshtml?highlight=1,15,16,17,28,31,34,37,43 )]
2017-04-10 08:52:07 +08:00
2017-06-22 23:46:18 +08:00
Examine the lambda expression used in the following HTML Helper:
2018-10-16 03:16:49 +08:00
`@Html.DisplayNameFor(model => model.Movies[0].Title)`
2017-06-22 23:46:18 +08:00
2018-10-16 03:16:49 +08:00
In the preceding code, the `DisplayNameFor` HTML Helper inspects the `Title` property referenced in the lambda expression to determine the display name. Since the lambda expression is inspected rather than evaluated, you don't receive an access violation when `model` , `model.Movies` , or `model.Movies[0]` are `null` or empty. When the lambda expression is evaluated (for example, `@Html.DisplayFor(modelItem => item.Title)` ), the model's property values are evaluated.
2017-06-22 23:46:18 +08:00
Test the app by searching by genre, by movie title, and by both.