C# 如何在ASP.NET Core razor页面上有多个带有多个处理程序的GET按钮?
我有一个带有搜索和下载功能的Razor文件C# 如何在ASP.NET Core razor页面上有多个带有多个处理程序的GET按钮?,c#,asp.net-core,razor,C#,Asp.net Core,Razor,我有一个带有搜索和下载功能的Razor文件 用户可以输入文本,单击搜索,然后过滤文件列表 用户还可以单击每个文件旁边的下载按钮 搜索功能可以正常工作 当用户单击下载时,将调用OnGet处理程序而不是OnGetDownload 这里是cshtml @page @model IndexModel @{ ViewData["Title"] = "Home page"; } <form> Search <input ty
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
<form>
Search
<input type="text" asp-for="FilenameFilter" class="form-control" placeholder="Filename ..." autocomplete="off" />
<button type="submit">Search</button>
</form>
<form>
Download
<table>
<thead>
<tr>
<th>File Name</th>
</tr>
</thead>
<tbody>
@foreach (var file in Model.Files)
{
<tr>
<td>@file</td>
<td><button type="submit" asp-page-handler="Download" asp-route-name=@file>Download</button>
</td>
</tr>
}
</tbody>
</table>
</form>
@page
@模型索引模型
@{
ViewData[“Title”]=“主页”;
}
搜寻
搜寻
下载
文件名
@foreach(Model.Files中的var文件)
{
@文件
下载
}
这里是C代码
公共类索引模型:PageModel
{
专用只读ILogger\u记录器;
[BindProperty(SupportsGet=true)]
公共字符串FilenameFilter{get;set;}
公共IEnumerable文件{get;set;}
公共索引模型(ILogger记录器)
{
_记录器=记录器;
Files=新列表{“file1.txt”、“file2.txt”、“file3.txt”};
}
公共互联网
{
//对于所有按钮事件(搜索和下载)都会调用此命令
_logger.LogInformation($“OnGet{FilenameFilter}”);
}
public void OnGetDownload(字符串名称)
{
//这不是调用请尝试使用Post方法提交表单并调用处理程序。请按以下方式修改代码:
<form method="post">
<button type="submit" asp-page-handler="Download" asp-route-name=@file>Download
</button>
</form>
下载
页面index.cshtml.cs中的代码(使用Post方法):
public void OnPostDownload(字符串名称)
{
//这不会被调用。您应该在每个循环的内部创建表单,并将其操作属性设置为OnGetDownload
这应该在每个循环的内部td
内部loop@ChetanRabpariya谢谢你的回复。不起作用(当我在循环中移动表单时)。你能用最新的代码更新问题代码并解释一下不起作用吗?@Chetan Ranpariya好的,我已经更新了。
<button type="submit" formaction="/?name=file3.txt&handler=Download">
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
<form>
Search
<input type="text" asp-for="FilenameFilter" class="form-control" placeholder="Filename ..." autocomplete="off" />
<button type="submit">Search</button>
</form>
Download
<table>
<thead>
<tr>
<th>File Name</th>
</tr>
</thead>
<tbody>
@foreach (var file in Model.Files)
{
<tr>
<td>@file</td>
<td>
<form method="get" action="OnGetDownload">
<button type="submit" asp-page-handler="Download" asp-route-name=@file>Download
</button>
</form>
</td>
</tr>
}
</tbody>
</table>
<form method="post">
<button type="submit" asp-page-handler="Download" asp-route-name=@file>Download
</button>
</form>
public void OnPostDownload(string name)
{
//This is not being called <---------------------------------
_logger.LogInformation($"OnGetDownload {name}");
}