Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Latest commit

 

History

History
138 lines (99 loc) · 4.27 KB

SearchPage-VSMac.md

File metadata and controls

138 lines (99 loc) · 4.27 KB

Add search to a Razor page using Visual Studio for Mac

The following tutorial is based on "Get started with Razor Pages in ASP.NET Core" from docs.microsoft.com.

Prerequisites

Adding Search to a page

In this tutorial, you're going to add search to the Index Page. By the end of this tutorial, you'll be able to search by genre and name.

Open Pages/Movies/Index.cshtml.cs and replace the OnGetAsync method with the following code:

public async Task OnGetAsync(string searchString)
{
    var movies = from m in _context.Movie
                 select m;

    if (!String.IsNullOrEmpty(searchString))
    {
        movies = movies.Where(s => s.Title.Contains(searchString));
    }

    Movie = await movies.ToListAsync();
}

Test search string

  • Run your application with F5 and open the Movies Page (http://localhost:{port}/Movies).

  • Append the query string to the end ?searchString=[Film Title] (for example, http://localhost:{port}/Movies?searchString=panther)

Add a Search Box

Search by Title

  1. Open the Pages/Movies/Index.cshtml file and add the <form> element as shown in the following code:

    <h1>Index</h1>
    
    <p>
        <a asp-page="Create">Create New</a>
    </p>
    <form>
        <p>
            Movie Title: <input type="text" name="SearchString"/>
            <input type="submit" value="Filter"/>
        </p>
    </form>
  2. Run the application by selecting Debug > Run without Debugging on the main menu.

  3. If you haven't created a few movies, go ahead and do that now.

  4. Enter a film title in the search box.

Search by Genre

  1. Open the Pages/Movies/Index.cshtml.cs file and add the following code::

    public class IndexModel : PageModel
    {
        private readonly RazorPagesMovie.Data.RazorPagesMovieContext _context;
    
        public IndexModel(RazorPagesMovie.Data.RazorPagesMovieContext context)
        {
            _context = context;
        }
    
        public IList<Movie> Movie { get;set; } = default!;
        public SelectList Genres;
        public string MovieGenre { get; set; }
  2. Update the OnGetAsync method on that same file:

    public async Task OnGetAsync(string movieGenre,string searchString)
    {
        IQueryable<string> genreQuery = from m in _context.Movie
                                orderby m.Genre
                                select m.Genre;
    
        var movies = from m in _context.Movie
                    select m;
    
        if (!String.IsNullOrEmpty(searchString))
        {
            movies = movies.Where(s => s.Title.Contains(searchString));
        }
    
        if (!String.IsNullOrEmpty(movieGenre))
        {
            movies = movies.Where(x => x.Genre == movieGenre);
        }
        Genres = new SelectList(await genreQuery.Distinct().ToListAsync());
        Movie = await movies.ToListAsync();
    }
  3. Open the Pages/Movies/Index.cshtml file and update the form element code:

    <form>
        <p>
            <select asp-for="MovieGenre" asp-items="Model.Genres">
                <option value="">All</option>
            </select>
            
            Movie Title: <input type="text" name="SearchString">
            <input type="submit" value="Filter"/>
        </p>
    </form>
  • Run the application by selecting Debug > Run without Debugging on the main menu.

  • Navigate to https://localhost:{port}/Movies

Mission accomplished!

You've built your first Razor Page application with C# and ASP.NET Core.