I have this method in Razor Page code behind which I would like to call using JQuery Ajax
[HttpPost]
public IActionResult OnPostPopulateMovieDate(int selectedMovieId)
{
return new JsonResult("json result");
}
This is my JQuery code to call the method
$.ajax({
contentType: "application/json",
dataType: 'json',
type: "POST",
data: {selectedMovieId:this.selectedIndex},
url: "/MovieDateHall/Create?handler=PopulateMovieDate",
success: function () {
console.log('ok')
}
});
I changed my url to url: "/MovieDateHall/OnPostPopulateMovieDate",
but still no success.
I put a breakpoint in PopulateMovieDate
method and debug it. But it doesn’t reach the breakpoint.
MovieDateHall
is the sub-folder name under Pages
.
2
Answers
What error are you getting? Did you check the browser console for any javascript error? Please also check the following line exists in your RegisterRoutes method:
routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”);
In razor page to call ajax method, you should to ensure the following two points :
(you can also refer to this link )
According to these, you can change your code as follow: