Javascript 如何将object类型的json数组传递给MVC控制器类
我试图通过一个JSON数组传递给我的控制器。我希望能够将数据从一个JSON数组发布到数据库,该数组通过客户端的javascript填充并传递给控制器。但是,它没有在控制器中接收任何内容。我的控制器收到一个空值作为参数 以下是我的控制器:Javascript 如何将object类型的json数组传递给MVC控制器类,javascript,c#,arrays,json,asp.net-mvc,Javascript,C#,Arrays,Json,Asp.net Mvc,我试图通过一个JSON数组传递给我的控制器。我希望能够将数据从一个JSON数组发布到数据库,该数组通过客户端的javascript填充并传递给控制器。但是,它没有在控制器中接收任何内容。我的控制器收到一个空值作为参数 以下是我的控制器: [HttpPost] [SiteAuthorize] public ActionResult SaveDashboard(List<Dashboard> dashboards) { try
[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);
}
}
[HttpPost]
[网站授权]
公共操作结果保存仪表板(列表仪表板)
{
尝试
{
字符串结果=”;
返回Json(新的{Result=Result,Message=”“},JsonRequestBehavior.AllowGet);
}
捕获(例外情况除外)
{
返回Json(新的{Result=“Error”,Message=ex.Message},JsonRequestBehavior.AllowGet);
}
}
以下是我的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");
}
});
var仪表盘={
“仪表板名称”:“”,
“宽度”:“”,
“高度”:“”,
“仪表板单元”:[
{
“DashCellId”:“”,
“x”:“,
“y”:“,
“DashWidth”:“”,
“DashHeight”:“”,
“colspan”:“”,
“行跨度”:“”,
“活动”:“”,
“CellValue”:“”,
“cellClass”:“”,
“previousElementSibling”:”
}
]
};
var tablestructure=$(“#tablestructure”).val();
var savename=document.getElementById(“namedash”).value;
var table=document.getElementById(“表格”);
变量宽度=table.clientWidth;
变量高度=table.clientHeight;
var DashboardCell=[];
dashboards.DashboardName=保存名称;
仪表盘。宽度=宽度;
仪表盘。高度=高度;
for(变量i=0,行;行=表。行[i];i++)
{
//遍历行
//可以使用for循环中指定的“row”变量访问行
for(var j=0,col;col=row.cells[j];j++)
{
对于(变量x=0;x0){
var previousElementSibling=col.previousElementSibling.id;
}
否则{
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);
//遍历列
//可以使用for循环中指定的“col”变量访问列
}
}
dashboards.DashboardCell=仪表板单元格;
$.ajax({
url:“/KPIReportAdmin/SaveDashboard”,
类型:“POST”,
数据类型:“json”,
contentType:“应用程序/json”,
数据:{dashboards:JSON.stringify(dashboards)},
成功:功能(结果)
{
},
错误:函数(结果){
警报(“失败”);
}
});
以下是我的班级:
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; }
}
公共类仪表板单元
{
公共整数宽度{get;set;}
公共整数高度{get;set;}
公共bool活动{get;set;}
公共int colspan{get;set;}
公共int行span{get;set;}
公共整数x{get;set;}
公共整数y{get;set;}
公共字符串CellValue{get;set;}
公共字符串DashCellId{get;set;}
公共字符串cellClass{get;set;}
public int previouselement同级{get;set;}
}
公共类仪表板
{
公共字符串名称{get;set;}
公共整数高度{get;set;}
公共整数宽度{get;set;}
public int UserID{get;set;}
公用列表仪表板单元格{get;set;}
}
我希望在SaveDashboard中接收仪表板列表,但我得到空值,有两种方法可以使用AJAX将JS对象作为
list
集合传递:
1) 使用对象本身放置JSON.stringify
,并设置traditional:true
选项,只应设置单个参数:
$.ajax({
url: '/KPIReportAdmin/SaveDashboard',
type: "POST",
dataType: "json",
contentType: "application/json",
traditional: true,
data: JSON.stringify(dashboards),
success: function (result)
{
},
error: function (result) {
alert("Failed");
}
});
2) 通过使用带有传统:true
选项的$.param()
函数传递原始对象而不进行字符串化:
$.ajax({
url: '/KPIReportAdmin/SaveDashboard',
type: "POST",
dataType: "json",
data: $.param({ dashboards: dashboards }, true),
success: function (result)
{
},
error: function (result) {
alert("Failed");
}
});
发生异常是因为您正在传递一个JS对象,该对象包含数据为{dashboards:JSON.stringify(dashboards)}的JSON字符串,而控制器操作方法不知道如何将其解析为作为参数的列表
集合对象
相关问题:
使用AJAX将JS对象作为
List
集合传递有两种方法:
1) 使用对象本身放置JSON.stringify
,并设置traditional:true
选项,只应设置单个参数:
$.ajax({
url: '/KPIReportAdmin/SaveDashboard',
type: "POST",
dataType: "json",
contentType: "application/json",
traditional: true,
data: JSON.stringify(dashboards),
success: function (result)
{
},
error: function (result) {
alert("Failed");
}
});
2) 通过使用带有传统:true
选项的$.param()
函数传递原始对象而不进行字符串化:
$.ajax({
url: '/KPIReportAdmin/SaveDashboard',
type: "POST",
dataType: "json",
data: $.param({ dashboards: dashboards }, true),
success: function (result)
{
},
error: function (result) {
alert("Failed");
}
});
发生异常的原因是,您正在传递一个JS对象,该对象包含数据为{dashboards:JSON.stringify(dashboards)}的JSON字符串,而控制器操作方法不知道如何将其解析为作为参数的列表集合对象