Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Calling Razor Pages method via ajax not working

I’m trying to call a Razor Pages method from ajax but it ends up just calling the OnGet() method of the page. The url seems right as it’s

https://localhost:7100/?handler=SearchCityState&cityTerm=Det

The method I want to call is in the Index.cshtml.cs file and the name is correct. It’s defined as:

public async Task<IActionResult> SearchCityState(string cityTerm){
 ...
}

If I put that url in the browser it goes to my OnGet() method instead. Am I missing some configuration or something?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to prefix handler names with OnGet or OnPost, otherwise they are just any method as far as Razor Pages is concerned:

public async Task<IActionResult> OnGetSearchCityState(string cityTerm){
 ...
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading