Cannot pass data with ajax to razor pages

不羁的心 提交于 2019-12-02 04:47:22

page is essentially a reserved name when using ASP.NET Core 2.0 Razor Pages. The problem you have is actually quite simple, yet very subtle:

When it sees a query-string parameter, the ASP.NET Core 2.0 Razor Pages framework interprets this as the name of the current Razor Page - You can see that if you change your page parameter to type string; it'll actually contain the name of the current page. In your case, because the type is an int, the model-binding process cannot bind a string (the name) to a number, so sets it to 0.

One way to work around this issue is to use a different name. e.g.:

public IActionResult OnGetProducts(int pageNumber)

-and-

data: {
    pageNumber: page,
    handler: 'Products'
}

There's a fair bit of discussion around this issue on Github, but it's not specific to your scenario. I suggest you either search for or raise a new issue that specifically talks about using a query-string parameter in a Razor Page.

As an unrelated side note, you don't need to set the Content-Type header in your $.ajax request - This is used when you are sending content, which you're not.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!