简体   繁体   中英

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:

在此处输入图像描述

and that DeleteSeasonTrip() method looks like this:

在此处输入图像描述

Now, I have the following method setup:

在此处输入图像描述

Which lives in the class EditModel :

在此处输入图像描述

in the following structure:

在此处输入图像描述

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

在此处输入图像描述

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??

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

在此处输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM