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;}
}