ASP.NET Core Razor Pages Ajax 帖子使用 FromBody 为空

问题描述 投票:0回答:1

我正在开发 ASP.NET (6.0) Razor Pages 应用程序。其中一个页面需要使用 AJAX 发布数据,但是发布的数据始终为空。

我尝试在 Stack Overflow 上使用之前的相关问题,但我的问题仍然存在。

这是我的 Razor 页面 (cshtml)

@Html.AntiForgeryToken()

<script type="text/javascript">
$(function () {

$('button').on('click', function(){

const token = $('[name="__RequestVerificationToken"]').val();
var data = { "MyVar": "Simon", "MyNum": 21 };

$.ajax({
    url: '?handler=SaveOrder',
    method: "post",
    contentType: "application/json",
    dataType: 'application/json; charset=utf-8',
    headers: {
        "RequestVerificationToken" : token
    },
    data: JSON.stringify(data)
});
</script>

这是同一个 Razor 页面的我的 PageModel

如您所见,Ajax Post 正在工作,它命中了适当的处理程序方法,但没有发布数据。

请问有人可以帮忙吗?

jquery ajax asp.net-core razor-pages frombodyattribute
1个回答
0
投票

将评论(OP 在后续评论中验证为解决方案)转换为答案:

刚刚注意到:

SectionInputModel
的属性必须是
public
才能支持模型绑定。
Poul Bak 2022 年 10 月 21 日 12:37

© www.soinside.com 2019 - 2024. All rights reserved.