This is a migrated thread and some comments may be shown as answers.

DataSourceResult with DTOs in ASP.NET

1 Answer 1185 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Nse
Top achievements
Rank 1
Nse asked on 20 Jun 2016, 09:51 PM

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

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

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?

1 Answer, 1 is accepted

Sort by
0
Boyan Dimitrov
Telerik team
answered on 22 Jun 2016, 03:18 PM

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. 

Regards,
Boyan Dimitrov
Telerik
 
Get started with Kendo UI in days. Online training courses help you quickly implement components into your apps.
 
Tags
Data Source
Asked by
Nse
Top achievements
Rank 1
Answers by
Boyan Dimitrov
Telerik team
Share this question
or