Cannot pass data with ajax to razor pages

前端 未结 1 1105
一生所求
一生所求 2021-01-22 02:48

I am trying to send some data to razor page method, but the problem is that in method it takes always as 0.

Heres the code:

Razor Page:



        
相关标签:
1条回答
  • 2021-01-22 03:09

    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.

    0 讨论(0)
提交回复
热议问题