【问题标题】:Using Kendo UI DataSourceResponse with ASP.NET Web API DTOs将 Kendo UI DataSourceResponse 与 ASP.NET Web API DTO 一起使用
【发布时间】: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


【解决方案1】:

诀窍是将 IQueryableSkipTake 一起使用。

我无法测试代码,但我希望你能明白。

public class Invoice
{
    public int InvoiceID { get; set; }
}

public class InvoiceDTO
{
    public int InvoiceID { get; set; }
}

控制器

public ActionResult GetInvoices([DataSourceRequest] DataSourceRequest request)
{
    IQueryable<Invoice> invoices = _db.Invoices.Include("Client")
        .OrderBy(x => x.InvoiceDate);

    int pageSize = request.PageSize, 
        pageIndex = request.Page - 1, 
        total = invoices.Count();

    var models = invoices.Skip(pageIndex*pageSize).Take(pageSize)
        .Select(x => new InvoiceDTO
        {
            InvoiceID = x.InvoiceID
        })
        .ToList();

    var result = new DataSourceResult
    {
        Data = models,
        Total = total
    };

    return Json(result);
}

查看

@(Html.Kendo().Grid<HomeController.InvoiceDTO>()
    .Name("invoiceGrid")
    .Pageable(pageableBuilder => pageableBuilder.Refresh(true).PageSizes(new[]{5, 10, 25}))
    .Columns(columns =>
    {
        columns.Bound(d => d.InvoiceID).Width(125);
    })
    .DataSource(dataSource => dataSource
            .Ajax()
            .ServerOperation(true)
            .PageSize(10)
            .Read(read => read.Action("GetInvoices", "Invoices"))
            .Model(m => m.Id(x => x.InvoiceID)))
)

请原谅我的 C# 代码;我已经好几年没用VB了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-09
    • 2014-10-29
    • 2015-10-10
    • 2012-12-27
    • 1970-01-01
    • 2018-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多