系列文章目录:系列教程:使用 Core创建Razor Pages Web应用程序 - zhangpeterx的博客
系列教程代码的GitHub地址:ASP .Net Core Razor Pages MySQL Tutorial
上一个教程:ASP .NET Core Web Razor Pages系列教程五:更新Razor Pages页面
修改Pages / Movies / Index.cshtml.cs
代码如下:
using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using Microsoft.AspNetCore.Mvc;using Microsoft.AspNetCore.Mvc.RazorPages;using Microsoft.AspNetCore.Mvc.Rendering;using Microsoft.EntityFrameworkCore;using RazorPagesMovie.Models;namespace RazorPagesMovie.Pages.Movies{public class IndexModel : PageModel{private readonly RazorPagesMovie.Models.RazorPagesMovieContext _context;public IndexModel(RazorPagesMovie.Models.RazorPagesMovieContext context){_context = context;}public IList<Movie> Movie {get;set; }[BindProperty(SupportsGet = true)]public string SearchString {get; set; }// Requires using Microsoft.AspNetCore.Mvc.Rendering;public SelectList Genres {get; set; }[BindProperty(SupportsGet = true)]public string MovieGenre {get; set; }public async Task OnGetAsync(){var movies = from m in _context.Movieselect m;if (!string.IsNullOrEmpty(SearchString)){movies = movies.Where(s => s.Title.Contains(SearchString));}Movie = await movies.ToListAsync();}}}
现在可以通过浏览器访问来进行查询,访问网站: http://localhost:5000/Movies?searchString=Ghost
然后修改Pages / Movies / Index.cshtml
的第一行为:
@page "{searchString?}"
前面的路由约束允许将标题搜索为路由数据(URL段)而不是查询字符串值。将?在"{searchString?}"表示这是一个可选的路径参数。
现在访问: http://localhost:5000/Movies/Ghost
打开Pages / Movies / Index.cshtml文件,在代码<a asp-page="Create">Create New</a> </p>
后添加如下代码:
<form><p>Title: <input type="text" asp-for="SearchString" /><input type="submit" value="Filter" /></p></form>
现在多了一个搜索框:
Core Web Razor Pages系列教程七: 添加新的字段