我正在使用ASP.NET WebAPI Controller输出一些JSON,我将使用JQuery datatables plug-in在网页上使用这些JSON。以下控制器执行输出:
[System.Web.Http.AcceptVerbs("GET", "POST")]
[System.Web.Http.HttpGet]
public IEnumerable<AdminNotification> ListPendingNotifications()
{
var db = ApplicationContext.DatabaseContext.Database;
//Get list of notification objects to iterate over
var notifications = db.Query<AdminNotification>("SELECT * FROM rsiAdminNotification WHERE dateTimeClosed IS NULL ORDER BY dateTime DESC");
return notifications;
}
以下列格式输出数据:
[ { "adminNotificationTypeId" : 11,
"customerId" : 0,
"dateTime" : "2014-05-15T20:16:00Z",
"dateTimeClosed" : "0001-01-01T00:00:00"
},
{ "adminNotificationTypeId" : 7,
"customerId" : 0,
"dateTime" : "2014-05-06T11:31:00Z",
"dateTimeClosed" : "0001-01-01T00:00:00"
},
{ "adminNotificationTypeId" : 7,
"customerId" : 0,
"dateTime" : "2014-05-06T11:31:00Z",
"dateTimeClosed" : "0001-01-01T00:00:00"
}
]
然而,似乎jQuery数据表只接受JSON,如果它包含在父JSON对象中。所以我需要以某种方式转换控制器,使其输出:
{
"data": [
{ "adminNotificationTypeId" : 11,
"customerId" : 0,
"dateTime" : "2014-05-15T20:16:00Z",
"dateTimeClosed" : "0001-01-01T00:00:00"
},
{ "adminNotificationTypeId" : 7,
"customerId" : 0,
"dateTime" : "2014-05-06T11:31:00Z",
"dateTimeClosed" : "0001-01-01T00:00:00"
},
{ "adminNotificationTypeId" : 7,
"customerId" : 0,
"dateTime" : "2014-05-06T11:31:00Z",
"dateTimeClosed" : "0001-01-01T00:00:00"
}
]
}
有人可以建议如何将我的输出包装在一个包含JSON的对象中吗?
非常感谢。
更新1:
在下面尝试了@ SoftwareFactor的课程,这里是我所在的地方:
public DataTableResult<AdminNotification> ListPendingNotifications()
{
var db = ApplicationContext.DatabaseContext.Database;
var notifications = db.Query<AdminNotification>("SELECT * FROM rsiAdminNotification WHERE dateTimeClosed IS NULL ORDER BY dateTime DESC");
var something = new DataTableResult<AdminNotification>();
something.sEcho = 1;
something.iTotalRecords = 1;
something.iTotalDisplayRecords = 1;
something.aaData = notifications;
return something;
}
我似乎无法让something.aaData = notifications;
正确映射。
更新2
将IEnumerable映射到像这样的List,有效:
var notifications = db.Query<AdminNotification>("SELECT id FROM rsiAdminNotification WHERE dateTimeClosed IS NULL ORDER BY dateTime DESC");
var something = new DataTableResult<AdminNotification>();
something.sEcho = 1;
something.iTotalRecords = 1;
something.iTotalDisplayRecords = 1;
IEnumerable<AdminNotification> enumerable = notifications;
List<AdminNotification> asList = enumerable.ToList();
something.aaData = asList;
return something;
答案 0 :(得分:3)
我在所有项目中都使用DataTables,这很棒。这是我用于自己目的的通用类:
public class DataTableResult<T>
{
public int sEcho { get; set; }
public int iTotalRecords { get; set; }
public int iTotalDisplayRecords { get; set; }
public List<T> aaData { get; set; }
}
我只是实例化它,用结果填充aaData属性并从ApiController返回。
答案 1 :(得分:1)
使用DataTables并不像你在那里那么简单。要使DataTable工作,您需要定义控制器函数以接收和回复DataTables使用的结构化数据。我认为定义两个模型类来表示请求和响应对象是有帮助的。您的控制器方法应该将请求对象作为输入,然后它们应该返回响应对象。 Web-API将自动为您处理与JSON的序列化。
有关这两个类的结构,以及如何配置DataTables的其他属性,请参阅http://legacy.datatables.net/usage/server-side
数组成员aaData可以声明为IEnumerable<String[]>
。其余成员应该直截了当地宣布。不要忘记根据请求对象中的值在响应对象中设置sEcho。在过程中将sEcho强制转换为int以避免跨站点脚本攻击也是明智之举。
答案 2 :(得分:1)
看起来更新版本的DataTable使这更容易。 DataTableResult现在可以是
public class DataTableResult<T>
{
public IList<T> data { get; set; }
}
由于DataTable将接受list of objects now
答案 3 :(得分:1)
我今天也有这个问题。假设您已安装Newtonsoft.Json软件包,则可以将以下内容用于IHttpActionResult ...
return Json(new {data = notifications});