I'm trying to pass through a JSON array through to my controller. I want to be able to post data to the database from a JSON array which is populated and passed to the controller through javascript in the client side. However, it is not receiving anything in the controller. My controller received a null value as the parameter.
The following is my controller:
[HttpPost]
[SiteAuthorize]
public ActionResult SaveDashboard(List<Dashboard> dashboards)
{
try
{
string result = "";
return Json(new { Result = result, Message = "" }, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
return Json(new { Result = "Error", Message = ex.Message }, JsonRequestBehavior.AllowGet);
}
}
The following is my javascript:
var dashboards = {
"DashboardName": '',
"Width": '',
"Height": '',
"DashboardCell": [
{
"DashCellId": '',
"x": '',
"y": '',
"DashWidth": '',
"DashHeight": '',
"colspan": '',
"rowspan": '',
"Active": '',
"CellValue": '',
"cellClass": '',
"previousElementSibling": ''
}
]
};
var tablestructure = $("#TableStructure").val();
var savename = document.getElementById("namedash").value;
var table = document.getElementById("table");
var width = table.clientWidth;
var height = table.clientHeight;
var DashboardCell = [];
dashboards.DashboardName = savename;
dashboards.Width = width;
dashboards.Height = height;
for (var i = 0, row; row = table.rows[i]; i++)
{
//iterate through rows
//rows would be accessed using the "row" variable assigned in the for loop
for (var j = 0, col; col = row.cells[j]; j++)
{
for (var x = 0; x < col.attributes.length; x++)
{
if (col.attributes[x].localName == "colspan") {
var colspan = col.attributes[x].value;
}
else if (col.attributes[x].localName == "rowspan") {
var rowspan = col.attributes[x].value;
}
else if (col.attributes[x].localName == "class")
{
var cellClass = col.attributes[x].value;
}
}
var res = col.id.split(", ");
var x = parseInt(res[0]);
var y = parseInt(res[1]);
var DashHeight = col.clientHeight;
var DashWidth = col.clientWidth;
if (j > 0) {
var previousElementSibling = col.previousElementSibling.id;
}
else {
var previousElementSibling = '';
}
var DashCellID = col.id;
var CellValue = col.innerText;
var DashCell = { DashCellId: DashCellID, x: x, y: y, DashWidth: DashWidth, DashHeight: DashHeight, colspan: colspan, rowspan: rowspan, Active: 1, CellValue: CellValue, cellClass: cellClass, previousElementSibling: previousElementSibling };
DashboardCell.push(DashCell);
//iterate through columns
//columns would be accessed using the "col" variable assigned in the for loop
}
}
dashboards.DashboardCell = DashboardCell;
$.ajax({
url: '/KPIReportAdmin/SaveDashboard',
type: "POST",
dataType: "json",
contentType: "application/json",
data: { dashboards: JSON.stringify(dashboards) },
success: function (result)
{
},
error: function (result) {
alert("Failed");
}
});
The following is my class:
public class DashboardCell
{
public int Width { get; set; }
public int Height { get; set; }
public bool Active { get; set; }
public int colspan { get; set; }
public int rowspan { get; set; }
public int x { get; set; }
public int y { get; set; }
public string CellValue { get; set; }
public string DashCellId { get; set; }
public string cellClass { get; set; }
public int previousElementSibling { get; set; }
}
public class Dashboard
{
public string Name { get; set; }
public int Height { get; set; }
public int Width { get; set; }
public int UserID { get; set; }
public List<DashboardCell> DashboardCell { get; set; }
}
I expect to receive the dashboard list in SaveDashboard but I'm getting null
dashboardsas string, try sending the object itself (no stringify)Invalid JSON primitive: object.when it is trying to access the controller. What could be causing that?