I've successfully implemented the following code:
Class (objects declared)
public class saveRow
{
public int proId { get; set; }
public string proName{get;set;}
}
Controller:
[HttpPost]
public virtual JsonResult SaveRow(saveRow input)
{ /* CODE HERE */}
JavaScript Object (Sent)
var test = {"proId" : 1, "proName" : "Test"}
JavaScript Ajax Call
$.ajax({
type: "POST",
url: "URL",
dataType: "json",
data: test,
traditional: true,
success: function (data, status, request) {
if (data.Error != undefined) {
alert("System Error: " + data.Error);
$(row).find('*').attr('disabled', false);
return;
}
},
error: function (request, status, error) {
console.log("ERROR");
}
});
The issue arises when attempting to send a list of rows instead of one at a time. To test this, I duplicated the object like so:
To test i took the same object and done
var test2 = []; test2.push(test); test2.push(test);
And now the object is as follows:
[{"proId" : 1, "proName" : "Test"},{"proId" : 1, "proName" : "Test"}]
My controller has been updated to:
[HttpPost]
public virtual JsonResult SaveRow(List<saveRow> input)
{ /* CODE HERE */}
Despite also trying with IEnumerable
, the "input" parameter in the controller remains null when sending a list of objects as JSON.
What could be causing this?
Solution Found:
public virtual JsonResult SaveRow(saveRow[] input)
Don't forget to include content type! And use JSON.stringify
!