1

I'm developing an Asp.Net (6.0) Razor Pages application. One of the pages needs to Post data using AJAX, however, the Post is always Null.

I have tried to use the previous related questions to this on Stackoverflow, but my problem still persists.

This is my Razor Page (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>

This is my PageModel for the same Razor Page

enter image description here

As you can see, the Ajax Post is working in that it hits the appropriate handler method, but no data is posted.

Can anyone help, please?

Thanks.

tcode
  • 5,055
  • 19
  • 65
  • 124
  • @PoulBak Hi, thanks for the suggestion, but that amendment doesn't work either. Now I don't even seen the model properties when posted, i.e. MyNum and MyVar. – tcode Oct 21 '22 at 12:16
  • 2
    Just noticed: The properties of `SectionInputModel` must be `public` to support model binding. – Poul Bak Oct 21 '22 at 12:37
  • @PoulBak that's exactly what the problem was. Can't believe I didn't notice that! D'oh! Thank you. – tcode Oct 21 '22 at 13:16

0 Answers0