I need to populate a jQuery datatable with my database data, but was very difficult to do so. Searching in the web, I believe I have found the cause, which would be the json format returned by my controller. The format should be as follows:
我需要使用我的数据库数据填充jQuery数据表,但是很难这样做。在网上搜索,我相信我找到了原因,这是我的控制器返回的json格式。格式应如下:
{
"data": [
{
"ID": "1",
"CODIGO": "CC0050",
"TEXTO": "USINAGEM"
},
{
"ID": "2",
"CODIGO": "CC0100",
"TEXTO": "MONTAGEM"
}]
}
but are returning in this format:
但是以这种格式返回:
[
{
"ID":24,
"CODIGO":"CC0050",
"TEXTO":"USINAGEM"
},
{
"ID":25,
"CODIGO":"CC0100",
"TEXTO":"MONTAGEM"
}
]
this is the controller code:
这是控制器代码:
[HttpGet]
public JsonResult GetAllCECOS()
{
using (RPIEntities contextObj = new RPIEntities())
{
try
{
var listaCECOS = contextObj.T0CECOS.ToList();
return Json(listaCECOS, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
throw ex;
}
}
}
2 个解决方案
#1
0
The cause is that you are returning the list of objects, and it's the list that is actually parsed in JSON.
原因是您返回的是对象列表,它是实际在JSON中解析的列表。
In that case, you want to return an object with the "data" property. It could be a named class as such:
在这种情况下,您希望返回具有“data”属性的对象。它可以是一个命名类:
public class MyReturningObject
{
public IEnumerable<T0CECOS> data { get; set; }
}
or an anonymous object :
或匿名对象:
return new { data = listaCECOS, JsonRequestBehavior.AllowGet };
Add the list in this object and parse it in JSON for the actual awaited JSON!
在此对象中添加列表,并使用JSON解析实际等待的JSON!
{
"data": [
{
"ID": "1",
"CODIGO": "CC0050",
"TEXTO": "USINAGEM"
},
{
"ID": "2",
"CODIGO": "CC0100",
"TEXTO": "MONTAGEM"
}]
}
#2
1
Thanks all,
I changed the return of my JsonResult mothod:
我更改了JsonResult方法的返回值:
[HttpGet]
public JsonResult GetAllCECOS()
{
using (RPIEntities contextObj = new RPIEntities())
{
try
{
var listaCECOS = contextObj.T0CECOS.ToList();
**return Json(listaCECOS, JsonRequestBehavior.AllowGet);**
}
catch (Exception ex)
{
throw ex;
}
}
}
to this:
return Json(new { data = listaCECOS }, JsonRequestBehavior.AllowGet);
#1
0
The cause is that you are returning the list of objects, and it's the list that is actually parsed in JSON.
原因是您返回的是对象列表,它是实际在JSON中解析的列表。
In that case, you want to return an object with the "data" property. It could be a named class as such:
在这种情况下,您希望返回具有“data”属性的对象。它可以是一个命名类:
public class MyReturningObject
{
public IEnumerable<T0CECOS> data { get; set; }
}
or an anonymous object :
或匿名对象:
return new { data = listaCECOS, JsonRequestBehavior.AllowGet };
Add the list in this object and parse it in JSON for the actual awaited JSON!
在此对象中添加列表,并使用JSON解析实际等待的JSON!
{
"data": [
{
"ID": "1",
"CODIGO": "CC0050",
"TEXTO": "USINAGEM"
},
{
"ID": "2",
"CODIGO": "CC0100",
"TEXTO": "MONTAGEM"
}]
}
#2
1
Thanks all,
I changed the return of my JsonResult mothod:
我更改了JsonResult方法的返回值:
[HttpGet]
public JsonResult GetAllCECOS()
{
using (RPIEntities contextObj = new RPIEntities())
{
try
{
var listaCECOS = contextObj.T0CECOS.ToList();
**return Json(listaCECOS, JsonRequestBehavior.AllowGet);**
}
catch (Exception ex)
{
throw ex;
}
}
}
to this:
return Json(new { data = listaCECOS }, JsonRequestBehavior.AllowGet);