【发布时间】:2014-06-17 14:55:53
【问题描述】:
我正在使用 AJAX 调用尝试将数组中的数组绑定到 C# 中的列表。第二个集合始终为空。
如果您不熟悉 DataTables,只需担心 dataTableOptions 的 AJAX 部分,因为它与 jQuery 相同。这是我目前所拥有的:
JavaScript
var dataRows = [{ RowCells: ["Test", "Test"] }];
var reportSectionContents = { DataRows: dataRows };
dataTableOptions = {
processing: true,
serverSide: true,
ajax: {
url: "/Data/ProcessDataTable",
type: "POST",
data: reportSectionContents
}
};
Action
[HttpPost]
public JsonResult ProcessDataTable(ReportSectionContents reportSectionContents)
ReportSectionContents
public class ReportSectionContents
{
public string Title { get; set; }
public ICollection<string> ColumnHeaders { get; set; }
public ICollection<ReportRow> DataRows { get; set; }
public ReportRow TotalRow { get; set; }
public ReportSectionGraphContents Graph { get; set; }
public ICollection<ReportSectionContents> LoopContents { get; set; }
}
ReportRow
public class ReportRow
{
public IList<string> RowCells { get; set; }
}
运行时,操作中的reportSecionContents 对象被填充,DataRows 属性在集合中有 1 个项目,这是预期的。该项目有一个ReportRow 的实例,但在该对象内,RowCells 属性始终为空。
我希望这将填充 2 个字符串项("Test"、"Test")。我错过了什么?
编辑
有了 arserbin3 的回答,我可以更进一步,但现在我收到了 AJAX 请求错误,即“无效的 JSON 原语:后跟 JSON 字符串”。我已经使用标准的 jQuery AJAX 对其进行了测试,它工作正常,所以我很确定这是一个 DataTables 问题(www.datatables.net)。我传递的字符串似乎不正确,但数据表试图解析的信息似乎不像是 JSON 格式
【问题讨论】:
-
我很好奇 IList 是否会导致问题。在某些情况下在 EF 中工作时,我遇到了 IList 未正确映射的问题。您是否尝试过将 IList 挂在 List 上?
-
我只试过 List,结果是一样的。也许这会导致同样的问题?编辑:刚刚尝试将其更改为集合,但我仍然遇到同样的问题
-
你是否在 ajax 调用中添加了 dataType: 'json' 以查看是否有影响?
标签: c# javascript jquery ajax json