Hey guys, I'm new to the world of web development and I've encountered a challenging issue. I have a complex object with numerous fields filled by a JavaScript function that needs to be passed to a C# HttpPost Call.
I attempted to use JSON.Stringify to convert all the data into a string and then made an Ajax call to send it over to the C# script. However, when the call initiates, my input is consistently Null, leaving me confused about where the problem lies - during the ajax call or inside the HttpPost method.
If anyone could assist this n00b in distress, I would greatly appreciate it!
Ajax call:
$.ajax({
type: "POST",
url: "/playlist",
data: jsonPlaylist
})
C# method:
public class JsonController : Controller
{
[HttpPost]
[Route("playlist")]
public Playlist getJson([FromBody]string playlist)
{
Playlist myPlaylist;
if (playlist == null)
{
return null;
}
else
{
myPlaylist = JsonConvert.DeserializeObject<Playlist>(playlist);
return myPlaylist;
}
}
}
Object Class:
public class Playlist
{
// Fields description here...
}
This is just a small snippet of the content being passed:
Sample json payload goes here...
}