【发布时间】:2016-06-21 16:37:07
【问题描述】:
我正在尝试使用一个对象填充 Kendo UI 网格,但是我使用的是 DTO 而不是在我的数据库中定义的实体框架对象。问题是我的 DataSourceResult 需要一个 IQueryable 形式的对象,而我的 DTO 是 List 类型。这是我的 Invoice 对象:
Public Class Invoice
<Key>
Public Property InvoiceID As Integer
Public Property Price As Double
Public Property AmountPaid As Double
Public Property Status As InvoiceStatus
<StringLength(10000)>
Public Property Memo As String
Public Property Client As Client
Public Property ClientID As Integer
End Class
这是我的 DTO:
Public Class InvoiceDTO
Public Property InvoiceID As Integer
Public Property InvoiceDate As Date
Public Property Price As Double
Public Property AmountPaid As Double
Public Property Status As String
Public Property Memo As String
Public Property Client As String
Public Property ClientID As Integer
End Class
我在我的 API 的 GET 中使用以下代码将对象返回到我的网格。我想返回 DataSourceResult 而不是 List,因为我可以使用分页和排序功能尽可能高效地从我的 API 返回数据。
<HttpGet, Route("api/client/all/invoices/{InvoiceStatus:int}")>
Public Function GetInvoices(requestMessage As HttpRequestMessage) As DataSourceResult
Dim Invoices As IEnumerable(Of Invoice) = _db.Invoices.Include("Client").OrderBy(Function(i) i.InvoiceDate)
Dim invDTOs As New List(Of InvoiceDTO)
For Each inv As Invoice In Invoices
Dim invDTO As New InvoiceDTO
invDTO.Client = inv.Client.Name
invDTO.ClientID = inv.ClientID
invDTO.Memo = inv.Memo
invDTO.InvoiceDate = inv.InvoiceDate
invDTO.Price = inv.Price
invDTO.AmountPaid = inv.AmountPaid
invDTO.Status = status(inv.Status)
invDTOs.Add(invDTO)
Next
Dim result As New DataSourceResult()
result.Data = invDTOs
result.Total = invDTOs.Count
Dim response As HttpResponseMessage = Request.CreateResponse(HttpStatusCode.Created, result)
Return response
End Function
我无法查询 DTO,因为它是一个列表。我可以将 DataSourceRequest 的 Data 属性设置为我的 DTO,但是我失去了内置的 DataSourceRequest 对象的分页功能,该对象与 Grid 配合得很好。如何将 DTO 与 DataSourceResult 一起使用?
【问题讨论】:
-
嗯,它只是一个列表,因为这就是您定义它的方式。是什么让您无法将其设为 IQueryable?此外,您可以使用 Automapper 真正将其缩减为“Dim invDTOs = _db.Invoices.Include("Client").OrderBy(Function(i) i.InvoiceDate).ProjectTo(Of InvoiceDTO)”
-
事实上,一旦您开始迭代发票以构建您的 DTO,由于尚未应用分页,因此会将它们全部恢复。
-
您还需要将请求对象应用于您的结果对象以获取分页:docs.telerik.com/kendo-ui/aspnet-mvc/vb#controller
标签: asp.net asp.net-web-api kendo-ui kendo-grid