ASP.NET Core 7 MVC中实现全站生效的食谱分类筛选功能
ASP.NET Core 7 食谱博客全站分类筛选实现问题
我正在开发基于ASP.NET Core 7的食谱博客网站,希望在导航栏中实现食谱分类筛选(如全部食谱、素食食谱、非素食食谱),让该筛选能作用于全站所有页面以优化用户体验。以下是我的相关代码:
FilterController.cs:
public class FilterController : Controller { private readonly ApplicationDbContext _context; public FilterController(ApplicationDbContext context) { _context = context; } [HttpPost] public async Task<IActionResult> Index(PostCategory? postCategory) { var post = from p in _context.Posts select p; // 分类筛选 if (postCategory != null) { post = _context.Posts.Where(c => c.PostCategory == postCategory); } return View(await post.ToListAsync()); } }
_TopNavbar.cshtml:
<nav class="navbar navbar-expand navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3 ps-10 fixed-top"> <div class="container-fluid"> <!-- 选项区 --> <div class="navbar-collapse collapse d-sm-inline-flex justify-content-between ps-3"> <!-- 下拉筛选 --> <div class="col-4"> <form asp-controller="Filter" asp-action="Index" id="category"> <select class="form-control" asp-items="@Html.GetEnumSelectList<PostCategory>()" name="postCategory" id="post-filter"> <option value="">全部</option> </select> <button class="btn btn-outline-success me-2" type="submit">筛选</button> <a class="btn btn-outline-danger" asp-area="" asp-controller="Post" asp-action="Index">重置</a> </form> </div> </div> </div> </nav>
内容的提问来源于stack exchange,提问作者ZNAXNOR
相关产品推荐
相关产品推荐

