Guest User

Untitled

a guest
Apr 27th, 2021
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.66 KB | None | 0 0
  1. // Unused usings removed.
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.AspNetCore.Mvc.RazorPages;
  4. using Microsoft.AspNetCore.Mvc.Rendering;
  5. using Microsoft.EntityFrameworkCore;
  6. using RazorPagesMovie.Models;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Threading.Tasks;
  10.  
  11.  
  12. namespace RazorPagesMovie.Pages.Movies
  13. {
  14.     public class IndexModel : PageModel
  15.     {
  16.         private readonly RazorPagesMovieContext _context;
  17.  
  18.         public IndexModel(RazorPagesMovieContext context)
  19.         {
  20.             _context = context;
  21.         }
  22.         public IList<Movie> Movie { get;set; }
  23.          [BindProperty(SupportsGet = true)]
  24.         public string SearchString { get; set; }
  25.         public SelectList Genres { get; set; }
  26.         [BindProperty(SupportsGet = true)]
  27.         public string MovieGenre { get; set; }
  28.  
  29.         public async Task OnGetAsync()
  30.         {
  31.             // Use LINQ to get list of genres.
  32.             IQueryable<string> genreQuery = from m in _context.Movie
  33.                                             orderby m.Genre
  34.                                             select m.Genre;
  35.  
  36.             var movies = from m in _context.Movie
  37.                         select m;
  38.  
  39.             if (!string.IsNullOrEmpty(SearchString))
  40.             {
  41.                 movies = movies.Where(s => s.Title.Contains(SearchString));
  42.             }
  43.  
  44.             if (!string.IsNullOrEmpty(MovieGenre))
  45.             {
  46.                 movies = movies.Where(x => x.Genre == MovieGenre);
  47.             }
  48.             Genres = new SelectList(await genreQuery.Distinct().ToListAsync());
  49.             Movie = await movies.ToListAsync();
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment