Search code examples
c#ajaxrazor-pages

Razor Pages .Net Core - Calling C# method from Javascript / AJax


I have looked at a good few examples online of how to contact a .Net Core C# method from an Ajax call in Razor Pages - however having gone through a bunch of examples I can't seem to figure out why I can't get it to work for my scenario... Which I dont think is anything special!

I have a button to call this method on click:

enter image description here

and that DeleteSeasonTrip() method looks like this:

enter image description here

Now, I have the following method setup:

enter image description here

Which lives in the class EditModel:

enter image description here

in the following structure:

enter image description here

If I try to call the ajax request I get the follwoing:

enter image description here

Am I missing a crucial piece of the puzzle here? I feel I have followed the examples I have seen close enuogh, however I am really quite new to Razor Pages for Core so think I am just missing something simple!?


Solution

  • Thanks to Mike Brind comment on the original question, I have gone through this page and applied the Request Verification Token and now all works a treat:

    https://www.mikesdotnetting.com/article/336/ajax-posts-in-razor-pages-and-http-400-errors

    enter image description here

    With this RVT in place I am now able to hit the method in code behind and the request completes successfully.