我有一个小的 ASP.NET Core Razor Pages 项目。我正在制作一个带有基本搜索功能的简单列表显示页面。在我的模型中,我有 4 个页面处理程序(其中 2 个被添加用于调试目的):
public async Task OnGetAsync()
{
Posting = await _context.Postings
.Include(p => p.ItemDetails).Include(p => p.Owner).ToListAsync();
}
public async Task OnPostAsync()
{
Posting = await _context.Postings
.Include(p => p.ItemDetails).Include(p => p.Owner).ToListAsync();
}
public async Task<PageResult> OnGetSearchAsync(String search)
{
if (String.IsNullOrEmpty(search))
{
search = search.Trim();
Posting = await _context.Postings.Where(p => p.ItemDetails.ItemName.Contains(search)).ToListAsync();
}
return Page();
}
public async Task<PageResult> OnPostSearchAsync(String search)
{
if (!String.IsNullOrEmpty(search))
{
search = search.Trim();
Posting = await _context.Postings
.Where(p => p.ItemDetails.ItemName.Contains(search)).ToListAsync();
}
return Page();
}
当表单指定method="post"为 时asp-page-handler="search",表单会调用正确的处理程序 ( OnPostSearchAsync(String search))。但是,当表单指定method="get"为 时asp-page-handler="search",表单会调用错误的处理程序 ( OnGetAsync())。这是故意的吗?如果是这样,我如何在使用该GET方法时调用自定义处理程序?也许不需要使用自定义处理程序,但我认为如果我选择的话,我应该可以。
这是.cshtml文件中的相关代码:
<div id="posting_search_bar_container">
<form method="get" asp-page-handler="search">
<input type="text" name="search" />
<input type="submit" value="Ara" />
</form>
</div>
<div id="posting_list_container">
@if (Model.Posting != null)
{
@foreach (var posting in Model.Posting)
{
<partial name="./Partials/_Posting" model="new Pages.Postings.Partials.PostingModel(posting);" />
}
}
</div>
慕妹3242003
相关分类