我有一个像这样的类(class):
public class Group
{
public int RecID { get; set; }
public int GroupID { get; set; }
public int Name { get; set; }
}
我得到了这些项目的 IList:IList<Group>()
;
现在我正在尝试返回此列表的 javascript 数组,以便我可以将它与 jquery datatables 一起使用。
所以我在 Controller 中尝试了以下操作:
return Json(CacheHelper.Groups, JsonRequestBehavior.AllowGet);
但这给了我以下结果:
[
{"GroupID":"1","Name":"Group 1","RecID":5637144589},
{"GroupID":"2","Name":"Group 2","RecID":5637145326}
]
如何让它与数据表一起使用?
我尝试过以下方法:
$('#example').dataTable( {
"ajax": '/ajax/data.json'
"columns": [
"Name",
"GroupID",
"RecID"
]
} );
但是这无法加载数据。我认为这可能是因为数据的 json 格式错误,因为它需要像这样的数据
预期数据格式
{
data: [
1,
"Group 1",
5637144589
],
[
2,
"Group 2",
5637145326
]
}
所以我尝试了以下 linq 来更改数据:
CacheHelper.Groups.Select(x => new string[] { x.GroupID, x.Name, x.RecID });
但这给了我一个数组数组,没有 data
少量。
[
["1","Group 1",5637144589],
["2","Group 2",5637145326]
]
有什么办法可以用linq获取数据位吗?
所以我想问题是
- 有没有办法使用第一个 json 结果数据集并让它与 js 一起使用
如果没有
- 有什么方法可以使用 linq 使数据集看起来像预期的数据
最佳答案
您需要使用以下初始化代码来匹配您的数据结构:
$('#example').dataTable( {
"ajax": {
"url": '/ajax/data.json',
"dataSrc": ""
},
"columns": [
{ "data" : "Name" },
{ "data" : "GroupID" },
{ "data" : "RecID" }
]
});
来自dataSrc属性说明:
Note that if your Ajax source simply returns an array of data to display, rather than an object, set this parameter to be an empty string.
关于javascript - 获取数据集以使用数据表 ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30509626/