获取数据集以使用数据表 ajax
Get dataset to work with datatables ajax
我有一个 class 像这样:
public class Group
{
public int RecID { get; set; }
public int GroupID { get; set; }
public int Name { get; set; }
}
我得到了这些项目的 IList:IList<Group>()
;
现在我正在尝试 return 这个列表的 javascript 数组,以便我可以将它与 jquery datatables
一起使用
所以我在控制器中尝试了以下操作:
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.
我有一个 class 像这样:
public class Group
{
public int RecID { get; set; }
public int GroupID { get; set; }
public int Name { get; set; }
}
我得到了这些项目的 IList:IList<Group>()
;
现在我正在尝试 return 这个列表的 javascript 数组,以便我可以将它与 jquery datatables
一起使用所以我在控制器中尝试了以下操作:
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.