--- uid: mvc/overview/getting-started/introduction/adding-search title: "Search | Microsoft Docs" author: Rick-Anderson description: "" ms.author: aspnetcontent manager: wpickett ms.date: 05/22/2015 ms.topic: article ms.assetid: df001954-18bf-4550-b03d-43911a0ea186 ms.technology: dotnet-mvc ms.prod: .net-framework msc.legacyurl: /mvc/overview/getting-started/introduction/adding-search msc.type: authoredcontent --- Search ==================== by [Rick Anderson](https://github.com/Rick-Anderson) ## Adding a Search Method and Search View In this section you'll add search capability to the `Index` action method that lets you search movies by genre or name. ## Updating the Index Form Start by updating the `Index` action method to the existing `MoviesController` class. Here's the code: [!code-csharp[Main](adding-search/samples/sample1.cs?highlight=1,6-9)] The first line of the `Index` method creates the following [LINQ](https://msdn.microsoft.com/en-us/library/bb397926.aspx) query to select the movies: [!code-csharp[Main](adding-search/samples/sample2.cs)] The query is defined at this point, but hasn't yet 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, using the following code: [!code-csharp[Main](adding-search/samples/sample3.cs)] The `s => s.Title` code above is a [Lambda Expression](https://msdn.microsoft.com/en-us/library/bb397687.aspx). Lambdas are used in method-based [LINQ](https://msdn.microsoft.com/en-us/library/bb397926.aspx) queries as arguments to standard query operator methods such as the [Where](https://msdn.microsoft.com/en-us/library/system.linq.enumerable.where.aspx) method used in the above code. LINQ queries are not executed when they are defined or when they are modified by calling a method such as `Where` or `OrderBy`. Instead, query execution is deferred, which means that the evaluation of an expression is delayed until its realized value is actually iterated over or the [`ToList`](https://msdn.microsoft.com/en-us/library/bb342261.aspx) method is called. In the `Search` sample, the query is executed in the *Index.cshtml* view. For more information about deferred query execution, see [Query Execution](https://msdn.microsoft.com/en-us/library/bb738633.aspx). > [!NOTE] > The [Contains](https://msdn.microsoft.com/en-us/library/bb155125.aspx) method is run on the database, not the c# code above. On the database, [Contains](https://msdn.microsoft.com/en-us/library/bb155125.aspx) maps to [SQL LIKE](https://msdn.microsoft.com/en-us/library/ms179859.aspx), which is case insensitive. Now you can update the `Index` view that will display the form to the user. Run the application and navigate to */Movies/Index*. Append a query string such as `?searchString=ghost` to the URL. The filtered movies are displayed. ![SearchQryStr](adding-search/_static/image1.png) If you change the signature of the `Index` method to have a parameter named `id`, the `id` parameter will match the `{id}` placeholder for the default routes set in the *App\_Start\RouteConfig.cs* file. [!code-json[Main](adding-search/samples/sample4.json)] The original `Index` method looks like this:: [!code-csharp[Main](adding-search/samples/sample5.cs)] The modified `Index` method would look as follows: [!code-csharp[Main](adding-search/samples/sample6.cs?highlight=1,3)] You can now pass the search title as route data (a URL segment) instead of as a query string value. ![](adding-search/_static/image2.png) However, you can't expect users to modify the URL every time they want to search for a movie. So now you 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 your `Index` method takes a string parameter named `searchString`: [!code-csharp[Main](adding-search/samples/sample7.cs)] Open the *Views\Movies\Index.cshtml* file, and just after `@Html.ActionLink("Create New", "Create")`, add the form markup highlighted below: [!code-cshtml[Main](adding-search/samples/sample8.cshtml?highlight=12-15)] The `Html.BeginForm` helper creates an opening `