3

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 =>
        {
        });
5
  • I do this in the Ajax method.. data: JSON.stringify(dataType), then in the controller action I take out [FromBody] Commented Feb 17, 2017 at 12:08
  • I think there's other jiggery you have to do in .net core to make it work without [FromBody] (I've tried your suggestion anyway though, still all nulls) Commented Feb 17, 2017 at 12:15
  • You're posting to /Diary/Index but the function is called Save. Is this just an error in the post here or are you running this specific code? Commented Feb 17, 2017 at 12:23
  • Error in the post sorry, thanks Commented Feb 17, 2017 at 13:06
  • Why did you write contentType: dataType? You cannot reference the property above it like that. Commented Feb 17, 2017 at 13:37

2 Answers 2

7

You need to stringify the data that you are sending to the server to convert it to JSON because that's the content type header you indicated:

data: JSON.stringify(data)
Sign up to request clarification or add additional context in comments.

1 Comment

What the ACTUAL hell?! I spent hours on this going round and round various combinations of this - I have previously added JSON.stringify, and it's not worked...so what have you done? C'mon, own up, you've been here?
-1


Please Try This Solution :
In Jquery File :

var data = {firstname : 'ali', lastname: 'alavi', age: 31};
var formData = new FormData();
formData.append('data',JSON.stringify(data));

$.ajax({
        type: "POST",        
        url: controllerUrl, //something like ~/[ControllerName]/[ActionName]
        data: formData,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        processData: false,
        contentType: false,
        success: function (data) {
            if (data.success)
                // ...
            else
                // ...
        },
        error: function () {
            // log error ...
        }
    });

Then In Your Controller try :

using Newtonsoft.Json;
.
.
.
 [HttpPost]
 public JsonResult CreateServicesPost(string data)
 {
     var personModel = new PersonModel();
     personModel = JsonConvert.DeserializeObject<PersonModel>(data);
      // ...
 }


And Your Perso Model Is :

public class CreateServicePostModel
{
        public string Firstname{ get; set; }
        public string Lastname { get; set; }
        public int Age { get; set; }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.