【问题标题】:ASP.NET Core Razor Pages button is not workingASP.NET Core Razor 页面按钮不起作用
【发布时间】:2018-03-12 15:42:21
【问题描述】:

我有一个 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)
{                
}

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

【问题讨论】:

    标签: asp.net razor-pages


    【解决方案1】:

    您的问题是您尝试附加不是 html 但它是标签助手的 html。该框架将获取 asp-route-data 和 asp-page-handler 以生成按钮的 formaction。

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

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

    【讨论】:

    • 谢谢!但我还有另一个问题。当我单击我的按钮时 - 出现此错误:Microsoft.AspNetCore.Antiforgery.AntiforgeryValidationException:在表单字段“__RequestVerificationToken”或标头值“RequestVerificationToken”中均未提供所需的防伪请求令牌。 в Microsoft.AspNetCore.Antiforgery.Internal.DefaultAntiforgery.d__9.MoveNext()
    • 这是因为您必须将防伪令牌放入表单中。
    猜你喜欢
    • 2020-12-10
    • 2019-08-15
    • 2020-06-03
    • 1970-01-01
    • 2019-04-19
    • 2022-01-10
    • 2018-05-07
    • 2019-02-04
    • 2022-01-20
    相关资源
    最近更新 更多