DataSourceResult with DTOs in ASP.NET

2 posts, 0 answers
  1. Nse
    Nse avatar
    12 posts
    Member since:
    Feb 2016

    Posted 20 Jun 2016 Link to this post

    I'm attempting to populate a Kendo UI Grid with an object, however I'm using a DTO instead of the Entity Framework object that's defined in my database. The problem is that my DataSourceResult expects an object in the form of an IQueryable and my DTO is of a type List. Here is my Invoice object:

    Public Class Invoice
        Public Property InvoiceID As Integer
        Public Property Price As Double
        Public Property AmountPaid As Double
        Public Property Status As InvoiceStatus
        Public Property Memo As String
        Public Property Client As Client
        Public Property ClientID As Integer
    End Class

    And here is my 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

    I'm using the following code in my GET in my API to return the object to my Grid. I want to return a DataSourceResult as opposed to a List because I can use the paging and sorting functions to return data from my API as efficiently as possible.

    <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)
        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

    I can't query the DTO because it's a list. I can set the Data property of the DataSourceRequest to my DTO, however then I lose the paging functionality of the DataSourceRequest object built in that works well with the Grid. How do I use a DTO with the DataSourceResult?

  2. Boyan Dimitrov
    Boyan Dimitrov avatar
    1966 posts

    Posted 22 Jun 2016 Link to this post

    Hello Nse,

    I guess what you are looking for is to use the ToDataSourceResult extension method to convert the Products to a Kendo.Mvc.UI.DataSourceResult object. This extension method will page, filter, sort, or group your data using the information provided by the DataSourceRequest object. To use the ToDataSourceResult extension method, import the Kendo.Mvc.Extensions namespace. It works fine with IEnumerable including List. 

    Please refer to the ui-for-aspnet-mvc-examples/grid/webapi-crud/ example example. The ToDataSourceResult method allows to return a view model as shown below: 

    return Json(entities.ToDataSourceResult(request, ModelState, product => new ProductViewModel
               ProductID = product.ProductID,
               ProductName = product.ProductName,
               UnitsInStock = product.UnitsInStock

    Lastly, I noticed that  HttpResponseMessage is set to "Created", but it seems that this method returns all invoices, not the method for creating an invoice. 

    Boyan Dimitrov
    Get started with Kendo UI in days. Online training courses help you quickly implement components into your apps.
Back to Top