Skip to content

Latest commit

 

History

History
219 lines (128 loc) · 12.5 KB

File metadata and controls

219 lines (128 loc) · 12.5 KB
title author description ms.author ms.date no-loc uid
Add search to ASP.NET Core Razor Pages
rick-anderson
Shows how to add search to ASP.NET Core Razor Pages
riande
12/05/2019
Blazor
Identity
Let's Encrypt
Razor
SignalR
tutorials/razor-pages/search

Add search to ASP.NET Core Razor Pages

By Rick Anderson

::: moniker range=">= aspnetcore-3.0"

[!INCLUDE]

In the following sections, searching movies by genre or name is added.

Add the following highlighted properties to Pages/Movies/Index.cshtml.cs:

[!code-csharp]

  • SearchString: contains the text users enter in the search text box. SearchString has the [BindProperty] attribute. [BindProperty] binds form values and query strings with the same name as the property. (SupportsGet = true) is required for binding on GET requests.
  • Genres: contains the list of genres. Genres allows the user to select a genre from the list. SelectList requires using Microsoft.AspNetCore.Mvc.Rendering;
  • MovieGenre: contains the specific genre the user selects (for example, "Western").
  • Genres and MovieGenre are used later in this tutorial.

[!INCLUDE]

Update the Index page's OnGetAsync method with the following code:

[!code-csharp]

The first line of the OnGetAsync method creates a LINQ query to select the movies:

// using System.Linq;
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 property is not null or empty, the movies query is modified to filter on the search string:

[!code-csharp]

The s => s.Title.Contains() code is a Lambda Expression. Lambdas are used in method-based LINQ queries as arguments to standard query operator methods such as the Where method or Contains (used in the preceding code). LINQ queries are not executed when they're defined or when they're modified by calling a method (such as Where, Contains or OrderBy). Rather, query execution is deferred. That means the evaluation of an expression is delayed until its realized value is iterated over or the ToListAsync method is called. See Query Execution for more information.

Note

The Contains method is run on the database, not in the C# code. The case sensitivity on the query depends on the database and the collation. On SQL Server, Contains maps to SQL LIKE, which is case insensitive. In SQLite, with the default collation, it's case sensitive.

Navigate to the Movies page and append a query string such as ?searchString=Ghost to the URL (for example, https://localhost:5001/Movies?searchString=Ghost). The filtered movies are displayed.

Index view

If the following route template is added to the Index page, the search string can be passed as a URL segment (for example, https://localhost:5001/Movies/Ghost).

@page "{searchString?}"

The preceding route constraint allows searching the title as route data (a URL segment) instead of as a query string value. The ? in "{searchString?}" means this is an optional route parameter.

Index view with the word ghost added to the Url and a returned movie list of two movies, Ghostbusters and Ghostbusters 2

The ASP.NET Core runtime uses model binding to set the value of the SearchString property from the query string (?searchString=Ghost) or route data (https://localhost:5001/Movies/Ghost). Model binding is not case sensitive.

However, you can't expect users to modify the URL to search for a movie. In this step, UI is added to filter movies. If you added the route constraint "{searchString?}", remove it.

Open the Pages/Movies/Index.cshtml file, and add the <form> markup highlighted in the following code:

[!code-cshtml]

The HTML <form> tag uses the following Tag Helpers:

  • Form Tag Helper. When the form is submitted, the filter string is sent to the Pages/Movies/Index page via query string.
  • Input Tag Helper

Save the changes and test the filter.

Index view with the word ghost typed into the Title filter textbox

Search by genre

Update the OnGetAsync method with the following code:

[!code-csharp]

The following code is a LINQ query that retrieves all the genres from the database.

[!code-csharp]

The SelectList of genres is created by projecting the distinct genres.

[!code-csharp]

Add search by genre to the Razor Page

Update Index.cshtml as follows:

[!code-cshtml]

Test the app by searching by genre, by movie title, and by both.

Additional resources

[!div class="step-by-step"] Previous: Updating the pages Next: Adding a new field

::: moniker-end

::: moniker range="< aspnetcore-3.0"

[!INCLUDE]

In the following sections, searching movies by genre or name is added.

Add the following highlighted properties to Pages/Movies/Index.cshtml.cs:

[!code-csharp]

  • SearchString: contains the text users enter in the search text box. SearchString has the [BindProperty] attribute. [BindProperty] binds form values and query strings with the same name as the property. (SupportsGet = true) is required for binding on GET requests.
  • Genres: contains the list of genres. Genres allows the user to select a genre from the list. SelectList requires using Microsoft.AspNetCore.Mvc.Rendering;
  • MovieGenre: contains the specific genre the user selects (for example, "Western").
  • Genres and MovieGenre are used later in this tutorial.

[!INCLUDE]

Update the Index page's OnGetAsync method with the following code:

[!code-csharp]

The first line of the OnGetAsync method creates a LINQ query to select the movies:

// using System.Linq;
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 property is not null or empty, the movies query is modified to filter on the search string:

[!code-csharp]

The s => s.Title.Contains() code is a Lambda Expression. Lambdas are used in method-based LINQ queries as arguments to standard query operator methods such as the Where method or Contains (used in the preceding code). LINQ queries are not executed when they're defined or when they're modified by calling a method (such as Where, Contains or OrderBy). Rather, query execution is deferred. That means the evaluation of an expression is delayed until its realized value is iterated over or the ToListAsync method is called. See Query Execution for more information.

Note: The Contains method is run on the database, not in the C# code. The case sensitivity on the query depends on the database and the collation. On SQL Server, Contains maps to SQL LIKE, which is case insensitive. In SQLite, with the default collation, it's case sensitive.

Navigate to the Movies page and append a query string such as ?searchString=Ghost to the URL (for example, https://localhost:5001/Movies?searchString=Ghost). The filtered movies are displayed.

Index view

If the following route template is added to the Index page, the search string can be passed as a URL segment (for example, https://localhost:5001/Movies/Ghost).

@page "{searchString?}"

The preceding route constraint allows searching the title as route data (a URL segment) instead of as a query string value. The ? in "{searchString?}" means this is an optional route parameter.

Index view with the word ghost added to the Url and a returned movie list of two movies, Ghostbusters and Ghostbusters 2

The ASP.NET Core runtime uses model binding to set the value of the SearchString property from the query string (?searchString=Ghost) or route data (https://localhost:5001/Movies/Ghost). Model binding is not case sensitive.

However, you can't expect users to modify the URL to search for a movie. In this step, UI is added to filter movies. If you added the route constraint "{searchString?}", remove it.

Open the Pages/Movies/Index.cshtml file, and add the <form> markup highlighted in the following code:

[!code-cshtml]

The HTML <form> tag uses the following Tag Helpers:

  • Form Tag Helper. When the form is submitted, the filter string is sent to the Pages/Movies/Index page via query string.
  • Input Tag Helper

Save the changes and test the filter.

Index view with the word ghost typed into the Title filter textbox

Search by genre

Update the OnGetAsync method with the following code:

[!code-csharp]

The following code is a LINQ query that retrieves all the genres from the database.

[!code-csharp]

The SelectList of genres is created by projecting the distinct genres.

[!code-csharp]

Add search by genre to the Razor Page

Update Index.cshtml as follows:

[!code-cshtml]

Test the app by searching by genre, by movie title, and by both. The preceding code uses the Select Tag Helper and Option Tag Helper.

Additional resources

[!div class="step-by-step"] Previous: Updating the pages Next: Adding a new field

::: moniker-end