ASP.NET Core Razor Pages 按钮不起作用

ASP.NET Core Razor Pages button is not working

我有一个 ASP.NET Core Razor pPages 项目。我可能会为我的脚本使用按钮。

我有这个 html 标记:

<body>
    <div id="container"></div>
    <div id="progress" style="display:none">
        <h4>Loading...</h4>
    </div>
</body>

还有这个脚本:

function GetData() {
        $.ajax({
            type: 'GET',
            url: './Index?handler=ListOfPhoto',
            data: { "pageindex": pageIndex, "pagesize": pageSize },
            dataType: 'json',
            success: function (data) {
                if (data != null) {
                    for (var i = 0; i < data.length; i++) {
                        var row = data[i];                        

                        var html = '<img src="/Photos/' + row.idUser + '/' + row.imageName + '" class="img-responsive" />';
                        html += '<h2>' + row.name + '</h2>';

                        html += '<form method="POST"> <button type="submit" asp-route-data="foo" asp-page-handler="Way">Way</button> </form>';

                        $("#container").append(html);

                    }
                    pageIndex++;
                }
            },
            beforeSend: function () {
                $("#progress").show();
            },
            complete: function () {
                $("#progress").hide();
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert(xhr.status);
            }
        });

但是这个按钮不起作用:

html += '<form method="POST"> <button type="submit" asp-route-data="foo" asp-page-handler="Way">Way</button> </form>';

处理程序方法:

public void OnPostWay(string data)
{                
}

有没有人有任何想法,为什么它不起作用?

你的问题是你试图附加 html 这不是 html 但它是一个标签助手。该框架将采用 asp-route-data 和 asp-page-handler 来生成按钮的形式。

在您的示例中,您必须自己生成表单,因此您必须将按钮替换为以下代码。

html += '<form method="POST"> <button type="submit" formaction="/YourPageName?data=foo&amp;handler=Way">Way</button> </form>';