0

I'm currently working on an issue sending JSON data to My Controller.

I found out that when Passing an Object, which Contains a nested one, the nested object will be null. I can't figure out what I'm missing...

My serverside looks like this:

[HttpPost]
public ActionResult ApplyChanges(List<Change> pChanges)
{
    //the Issue occurs here in every object of pChanges:
    //IgnoreFlag was populated correctly, but the Detection Object is null...?
}

public class Change
{
  public Detection Detection { get; set; }
  public bool IgnoreFlag { get; set; }
}

My Clientside looks like this:

var data = [
  {
    "Detection": {
     "PropertyOld": 1,
     "PropertyNew": 2, 
    },
    "IgnoreFlag": true
  },
  {
    "Detection": {
     "PropertyOld": 3,
     "PropertyNew": 4, 
    },
    "IgnoreFlag": false
  }
]

$.ajax({
  type: "POST",
  url: "/Url/To/ApplyChanges",
  data: JSON.stringify({"pChanges": data}),
  contentType: "application/json",
  success: function (data, textStatus, jqXHR) {
    //do something here
});

Can someone help me to solve this issue?

5
  • try to not stringify, just pass as it is Commented Mar 20, 2016 at 14:57
  • Thanks for your reply. I tried it, but it doesn't work. Same result.. Commented Mar 20, 2016 at 15:04
  • Nothing is ignored when I JSON.stringify your object. jsfiddle.net/qz3vp6gx Commented Mar 20, 2016 at 15:15
  • @squint yes, I validated the json string myself on jslint.com before posting this question. Indeed, the JSON string is valid. That's exactly what I find weird about that issue... No Idea, what the reason that my mvc-controller ignores the nested object could be? Commented Mar 20, 2016 at 15:32
  • 2
    But you're saying that JSON.stringify is ignoring something. I'm just showing that it doesn't ignore anything. Based on the client-side code you provided, there's nothing wrong. Not sure what the server-side issue may be. Commented Mar 20, 2016 at 15:44

1 Answer 1

1

use

JSON.stringify(data) 

instead of using

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

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.