Reputation: 23
I have a problem with sending JSON data from MVC View to Controller, all I get to Controller is:
where my JSON I send in Url.Action looks like this: (I create it by my own adding array to array by .push() and then JSON.stringify )
[
[
{
"isOrdered":false,
"orderLineId":"4550",
"comment":"",
"orderId":"2789"
}
],
[
{
"isOrdered":false,
"orderLineId":"4551",
"comment":"",
"orderId":"2789"
}
]
]
I use JQuery ajax POST
$.ajax({
type: "POST",
url: "@Url.Action("SaveCheckboxesAndComments")",
data: JSON.stringify(array),
traditional: true,
contentType: "application/json; charset=utf-8",
dataType: 'json'
});
and my controller:
public class JsonData
{
public bool IsOrdered { get; set; }
public string OrderLineId { get; set; }
public string Comment { get; set; }
public int OrderId { get; set; }
}
[HttpPost]
public async Task<ActionResult> SaveCheckboxesAndComments(List<JsonData> jsonArray)
{...
Should I change something in my class JsonData or in JS to get values? Because somehow the "frame" is working.
Upvotes: 1
Views: 1454
Reputation: 41480
Some things to try:
POST
request to make sure that the url is correct and that the JSON being sent is what you expectJsonData
class with a [DataContract]
attribute, and each property with a [DataMember(Name = "Name")]
attribute - to make sure that you don't run into issues with casing (the property names in the class are all capitalized, and your json starts with lower case for each).[
]
brackets on your generated Json. So while your action is expecting List<JsonData> jsonArray
, it is being sent List<List<JsonData> jsonArray>
. See how it works removing the outermost square brackets from the json being sent.Upvotes: 1