繁体   English   中英

如何在 ASP.NET Core razor 页面上有多个带有多个处理程序的 GET 按钮?

[英]How do I have multiple GET buttons with multiple handlers on a ASP.NET Core razor page?

我有一个具有搜索和下载功能的 Razor 文件。

  1. 用户可以输入文本,单击搜索并过滤文件列表。
  2. 用户还可以单击每个文件旁边的下载按钮。

搜索功能有效。

当用户单击下载时,将调用 OnGet 处理程序而不是 OnGetDownload。

这是cshtml。

@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>

这里是 C# 代码。

public class IndexModel : PageModel
{
    private readonly ILogger<IndexModel> _logger;

    [BindProperty(SupportsGet = true)]
    public string FilenameFilter { get; set; }

    public IEnumerable<string> Files { get; set; }

    public IndexModel(ILogger<IndexModel> logger)
    {
        _logger = logger;

        Files = new List<string> { "file1.txt", "file2.txt", "file3.txt" };
    }

    public void OnGet()
    {
        //This gets called for all button events (search  and download)
        _logger.LogInformation($"OnGet {FilenameFilter}");

    }

    public void OnGetDownload(string name)
    {
        //This is not being called <---------------------------------
        _logger.LogInformation($"OnGetDownload {name}");
    }
}

在浏览器开发人员工具中,我可以看到路线似乎是正确的,即

<button type="submit" formaction="/?name=file3.txt&amp;handler=Download">

更新

如果我将第二个表单更改为<form method="post">并将处理程序更改为 Post,则调用下载处理程序。 但从语义上讲,这不是 post 方法。 我如何有 2 个获取处理程序?


更新

这是对 cshtml 文件的更新(针对 Chetan Ranpariya 的评论)。 问题仍然存在。

@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>

尝试使用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(string name)
{
    //This is not being called <---------------------------------
    _logger.LogInformation($"OnGetDownload {name}");
}

然后,截图如下:

在此处输入图片说明

有关使用处理程序的更多详细信息,请查看Razor Pages 中的处理程序方法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM