--- title: Adding Search | Microsoft Docs author: rick-anderson description: Shows how to add search to simple ASP.NET Core MVC app keywords: ASP.NET Core, ms.author: riande manager: wpickett ms.date: 03/07/2017 ms.topic: article ms.assetid: d69e5529-8ef6-4628-855d-200206d962b9 ms.technology: aspnet ms.prod: asp.net-core uid: tutorials/first-mvc-app/search --- # Adding Search By [Rick Anderson](https://twitter.com/RickAndMSFT) In this section you'll add search capability to the `Index` action method that lets you search movies by *genre* or *name*. Update the `Index` method with the following code: [!code-csharp[Main](start-mvc/sample/MvcMovie/Controllers/MoviesController.cs?name=snippet_1stSearch)] The first line of the `Index` action method creates a [LINQ](http://msdn.microsoft.com/en-us/library/bb397926.aspx) query to select the movies: ```csharp var movies = from m in _context.Movie select m; ``` The query is *only* defined at this point, it has **not** been run against the database. If the `searchString` parameter contains a string, the movies query is modified to filter on the value of the search string: [!code-csharp[Main](start-mvc/sample/MvcMovie/Controllers/MoviesController.cs?name=snippet_SearchNull)] The `s => s.Title.Contains()` code above is a [Lambda Expression](http://msdn.microsoft.com/en-us/library/bb397687.aspx). Lambdas are used in method-based [LINQ](http://msdn.microsoft.com/en-us/library/bb397926.aspx) queries as arguments to standard query operator methods such as the [Where](http://msdn.microsoft.com/en-us/library/system.linq.enumerable.where.aspx) method or `Contains` (used in the code above). LINQ queries are not executed when they are defined or when they are modified by calling a method such as `Where`, `Contains` or `OrderBy`. Rather, query execution is deferred. That means that the evaluation of an expression is delayed until its realized value is actually iterated over or the `ToListAsync` method is called. For more information about deferred query execution, see [Query Execution](http://msdn.microsoft.com/en-us/library/bb738633.aspx). Note: The [Contains](http://msdn.microsoft.com/en-us/library/bb155125.aspx) method is run on the database, not in the c# code shown above. On the database, [Contains](http://msdn.microsoft.com/en-us/library/bb155125.aspx) maps to [SQL LIKE](http://msdn.microsoft.com/en-us/library/ms179859.aspx), which is case insensitive. Navigate to `/Movies/Index`. Append a query string such as `?searchString=ghost` to the URL. The filtered movies are displayed. ![Index view](search/_static/ghost.png) If you change the signature of the `Index` method to have a parameter named `id`, the `id` parameter will match the optional `{id}` placeholder for the default routes set in *Startup.cs*. [!code-csharp[Main](start-mvc/sample/MvcMovie/Startup.cs?highlight=5&name=snippet_1)] You can quickly rename the `searchString` parameter to `id` with the **rename** command. Right click on `searchString` **> Rename**. ![Contextual menu](search/_static/rename.png) The rename targets are highlighted. ![Code editor showing the variable highlighted throughout the Index ActionResult method](search/_static/rename2.png) Change the parameter to `id` and all occurrences of `searchString` change to `id`. ![Code editor showing the variable has been changed to id](search/_static/rename3.png) The previous `Index` method: [!code-csharp[Main](start-mvc/sample/MvcMovie/Controllers/MoviesController.cs?highlight=1,8&name=snippet_1stSearch)] The updated `Index` method: [!code-csharp[Main](start-mvc/sample/MvcMovie/Controllers/MoviesController.cs?highlight=1,8&name=snippet_SearchID)] You can now pass the search title as route data (a URL segment) instead of as a query string value. ![Index view with the word ghost added to the Url and a returned movie list of two movies, Ghostbusters and Ghostbusters 2](search/_static/g2.png) However, you can't expect users to modify the URL every time they want to search for a movie. So now you'll add UI to help them filter movies. If you changed the signature of the `Index` method to test how to pass the route-bound `ID` parameter, change it back so that it takes a parameter named `searchString`: [!code-csharp[Main](start-mvc/sample/MvcMovie/Controllers/MoviesController.cs?highlight=1&name=snippet_1stSearch)] Open the *Views/Movies/Index.cshtml* file, and add the `