I've been trying to send an array of JSON objects to my controller action, but it keeps showing up as null.
Here's the JavaScript code I'm using:
function UpdateSelected() {
var items = {};
//Loop through grid / sub grids and create json array
$('.k-detail-row .k-grid').each(function () {
var grid = $(this).data("kendoGrid");
var selectedElements = grid.select();
for (var j = 0; j < selectedElements.length; j++) {
var item = grid.dataItem(selectedElements[j]);
var json = item.toJSON();
items = [].concat(items, json);
}
})
//shift the first empty space out
items.shift();
//items is ready to send
alert(JSON.stringify(items));
$.ajax({
cache: false,
url: '/Update/GetSchematicsForSelectedVariants',
type: 'GET',
data: JSON.stringify(items),
success: function (data) {
alert("success");
}
});
}
This is what the JSON array looks like:
https://i.stack.imgur.com/oRW3p.png
And here's my Controller Action:
public JsonResult GetSchematicsForSelectedVariants(List<VariantViewModel> vm)//vm coming in null
{
return Json(_dataAccessService.GetSchematicsForMacroVariants(vm),JsonRequestBehavior.AllowGet);
}
VariantViewModel Class:
public class VariantViewModel
{
public string Id { get; set; }
public string Variant { get; set; }
}
I can't figure out why my list is being passed in as null. I'm not very experienced in passing JSON objects to the controller, but I think I have the basics down. Any advice on where I might be going wrong would be greatly appreciated.
If anyone could help point me in the right direction, that would be fantastic!