All relevant code:
//JSON data
var dataType = 'application/json; charset=utf-8';
var data = {
FirstName: 'Andrew',
LastName: 'Lock',
Age: 31
}
console.log('Submitting form...');
$.ajax({
type: 'POST',
url: '/Diary/Save',
dataType: 'json',
contentType: dataType,
data: data,
success: function (result) {
console.log('Data received: ');
console.log(result);
}
});
[HttpPost]
public IActionResult Save([FromBody] Person person)
{
return Json(person);
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
"person" in the Save method is always null. This should work...it feels perhaps like a settings issue? Do I need to make changes here:
services.AddMvc(options =>
{
});
/Diary/Indexbut the function is calledSave. Is this just an error in the post here or are you running this specific code?contentType: dataType? You cannot reference the property above it like that.