1

I have an AJAX request that is sending a JSON object from a MVC View to a Controller using a POST request:

function sendData(subscriptionJson) {
                $.ajax({
                    type: "POST",
                    url: '@Url.Action("SubscribeSecurities", "Subscription")',
                    data: '{"subscriptions": ' + subscriptionJson + '}',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        console.log("success response: " + response.responseText);
                        alert("Hello: " + response.Name + " .\nCurrent Date and Time: " + response.DateTime);

                    },
                    failure: function (response) {
                        console.log("failure response: " + response.responseText);
                        alert(response.responseText);
                    },
                    error: function (response) {
                        console.log("error response: " + response.responseText);
                        alert(response.responseText);
                    }
                });
            }

The controller action has the following definition:

[HttpPost]
        public ActionResult SubscribeSecurities(string subscriptions)
        {

The JSON has the following format:

{
    "Subscriptions": {
        "Obj1": {
            "Value1": "3454234",
            "Value2": "345643564",
            "Value3": "665445",
            "Value4": "True"
        },
        "Obj2": {
            "Value1": "3454234",
            "Value2": "345643564",
            "Value3": "665445",
            "Value4": "True"
        },
        "Obj3": {
            "Value1": "3454234",
            "Value2": "345643564",
            "Value3": "665445",
            "Value4": "True"
        }
    }
}

What could be causing the problem?

EDIT

Here are the updates that I made after creating objects to store the values returned by the JSON POST request.

JSON

var test = {

                    "Obj1": {
                        "Value1": "3454234",
                        "Value2": "345643564",
                        "Value3": "665445",
                        "Value4": "True"
                    },
                    "Obj2": {
                        "Value1": "3454234",
                        "Value2": "345643564",
                        "Value3": "665445",
                        "Value4": "True"
                    },
                    "Obj3": {
                        "Value1": "3454234",
                        "Value2": "345643564",
                        "Value3": "665445",
                        "Value4": "True"
                    }

                }

Model to catch JSON

   public class RootObject {
    // Give this a better name.  RootObject is a horrible name.

        public IEnumerable<SubscriptionObj> Subscriptions { get; set; }
}

public class SubscriptionObj
{
    public Int64 Value1 {get;set;}
    public Int64 Value2 {get;set;}
    public Int64 Value3 {get;set;}
    public Boolean Value4 {get;set;}
}
Community
  • 1
  • 1
loremIpsum1771
  • 2,497
  • 5
  • 40
  • 87

1 Answers1

2

Your controller is expecting a string, but you are sending it a custom object.

Either change your json to a string, or change what your controller is expecting to an object that matches what is being sent... for example:

public class RootObject {
    // Give this a better name.  RootObject is a horrible name.

    public Subscriptions Subscriptions {get;set;} = new Subscriptions();
}


public class Subscriptions {
    public Subscription Obj1 {get;set;} = new Subscription();
    public Subscription Obj2 {get;set;} = new Subscription();
    public Subscription Obj3 {get;set;} = new Subscription();
}


public class Subscription {
    public Int64 Value1 {get;set;}=0;
    public Int64 Value2 {get;set;}=0;
    public Int64 Value3 {get;set;}=0;
    public Boolean Value4 {get;set;}=false;
}

Your MVC controller will automatically deserialize the incoming json string into a real object. If the deserialized object is NOT a string, it will not pass along the incoming json to your Action's string parameter.

Sam Axe
  • 33,313
  • 9
  • 55
  • 89
  • Ok, so do I need to create objects to represent the JSON object hierarchy or is there a data type in C# that can store JSON? – loremIpsum1771 Sep 19 '17 at 19:54
  • I just created those objects (with different names) but it still doesn't seem to be working. I added some attributes with default values to the Subscription object. Would that have caused an issue? – loremIpsum1771 Sep 19 '17 at 20:32
  • I can't see what you've done, so I can't tell you where you may have gone wrong. Did you change the Action's method signature to accept your new type? – Sam Axe Sep 19 '17 at 20:33
  • There is not a Json data type. Wouldn't it be cool if there was. – Sam Axe Sep 19 '17 at 20:34
  • I took out the `= new Subscriptions()` parts of the statements because they were causing intellisense errors – loremIpsum1771 Sep 19 '17 at 20:41
  • Ok, so what I just did was directly copy your classes and added them as models. I'm getting the data to come through now. The issue is, the classes have hard coded attributes. How would I structure the classes if I have a dynamic amount of subscription objects? – loremIpsum1771 Sep 19 '17 at 21:05
  • Change `Subscriptions` to be an Array `Subscriiption[]` or `IEnumerable`, and change your json to be an array of Subscription objects. – Sam Axe Sep 19 '17 at 22:23
  • It doesn't seem to be working. I just updated the post with the code I used. – loremIpsum1771 Sep 20 '17 at 14:16