Here is my code, does anyone know what is wrong here ?
We're missing code but what I can see shows you are not following the rules. Define a route
handlers which allows you to call the post method using the URL. Otherwise, you need to use a query string; ?handler=message.
Razor pages use a naming convention. A post method starts with "OnPost" followed by the method name used in the AJAX function.
The example below injects the antiforgery token. Using the form tag helper does the same thing.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Security.Policy;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace RazorDemo.Pages.ajax
{
public class JsonExModel : PageModel
{
public void OnGet()
{
}
public IActionResult OnPostMessage()
{
var message = new
{
t = "1"
};
return new JsonResult(message);
}
}
}
however its not hitting the break point, here is my code. Its for a shopping cart, built from scratch. Found at Pages >> Cart >> Index.cshtml
Remove the button click handler. My example code used a button for testing. Your design does not use a button... Also change the AJAX URL so it matches your design.
e.g. instead of using OnPostMessage() I use OnPostAsyncMessage() the latter does not work
I assume you think adding "Async" to the end of "OnPost" makes the method async. That's not the case. The OnPost, OnGet, OnPut, naming convention apply to the Razor Pages framework and HTTP. Asycn/await is a .NET language level programming pattern used
across all platforms.
Member
265 Points
1172 Posts
Json results return as UNDEFINED ???
Aug 27, 2020 01:38 PM|afrika|LINK
Hi guys
I am using Razor action results to post/receive Ajax updates, however its returning UNDEFINED
Here is my code, does anyone know what is wrong here ?
it does hit the breakpoint where I have the Json return page on the server, but the client page alert shows UNDEFINED
All-Star
53121 Points
23675 Posts
Re: Json results return as UNDEFINED ???
Aug 27, 2020 03:38 PM|mgebhard|LINK
We're missing code but what I can see shows you are not following the rules. Define a route handlers which allows you to call the post method using the URL. Otherwise, you need to use a query string; ?handler=message.
Razor pages use a naming convention. A post method starts with "OnPost" followed by the method name used in the AJAX function.
The example below injects the antiforgery token. Using the form tag helper does the same thing.
using System; using System.Collections.Generic; using System.Linq; using System.Reflection.Metadata.Ecma335; using System.Security.Policy; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; namespace RazorDemo.Pages.ajax { public class JsonExModel : PageModel { public void OnGet() { } public IActionResult OnPostMessage() { var message = new { t = "1" }; return new JsonResult(message); } } }
Member
265 Points
1172 Posts
Re: Json results return as UNDEFINED ???
Aug 27, 2020 05:30 PM|afrika|LINK
thank you
however its not hitting the break point, here is my code. Its for a shopping cart, built from scratch. Found at Pages >> Cart >> Index.cshtml
and here is my handler
All-Star
53121 Points
23675 Posts
Re: Json results return as UNDEFINED ???
Aug 27, 2020 05:59 PM|mgebhard|LINK
Remove the button click handler. My example code used a button for testing. Your design does not use a button... Also change the AJAX URL so it matches your design.
Member
265 Points
1172 Posts
Re: Json results return as UNDEFINED ???
Aug 27, 2020 09:20 PM|afrika|LINK
This works, but one more question
when I try to run it using Async it does not work ?
e.g. instead of using OnPostMessage() I use OnPostAsyncMessage() the latter does not work
All-Star
53121 Points
23675 Posts
Re: Json results return as UNDEFINED ???
Aug 28, 2020 01:28 AM|mgebhard|LINK
I assume you think adding "Async" to the end of "OnPost" makes the method async. That's not the case. The OnPost, OnGet, OnPut, naming convention apply to the Razor Pages framework and HTTP. Asycn/await is a .NET language level programming pattern used across all platforms.
Member
265 Points
1172 Posts
Re: Json results return as UNDEFINED ???
Aug 31, 2020 09:39 PM|afrika|LINK
thanks for the explanation