200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > ASP.NET Core Web Razor Pages系列教程六:添加搜索功能

ASP.NET Core Web Razor Pages系列教程六:添加搜索功能

时间:2021-12-13 12:43:50

相关推荐

ASP.NET Core  Web  Razor Pages系列教程六:添加搜索功能

系列文章目录:系列教程:使用 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系列教程七: 添加新的字段

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。