Invalid ODataV4Adaptor binding to latest .NET oData Web API

I have a very simple Blazor Grid using the ODataV4Adapator binding with the use of paging, filtering and sorting.  An example request goes to a custom .net core web API endpoint as follows:

.. localhost:5120/ca/queryodata?$count=true&$orderby=Risk&$skip=0&$top=5

I believe this is fine but the problem comes in on the API side.  I'm using the latest .NET Core oData package and following this tutorial:

Up & Running w/ OData in ASP.NET 6 - OData (microsoft.com)

Here is my controller method:

[EnableQuery]
[HttpGet("queryodata")]
public IQueryable<TrackingQueryResponseItem> QueryOData()
{
     return _dbContext.QryClientAcceptanceTrackings();
}

The problem is the endpoint returns the straight JSON result using all of the bells and whistles of oData syntax when Syncfusion's documentation states that it wants the results to look like the following:

{
    Items: [{..}, {..}, {..}, ...],
    Count: #
}

So my question is.  What should I be doing on the backend to get the proper transformation to occur?  I've not seen any Syncfusion example as to the proper back end implementation of this feature.  

Thanks,
- JEFF



3 Replies

RN Rahul Narayanasamy Syncfusion Team November 19, 2021 03:35 AM UTC

Hi Jeff, 

Greetings from Syncfusion. 

We have validated your query and we suspect that you are facing complexities while binding the data to the Grid using ODataV4Adaptor. Please find the below documentation links for your reference. 


You need to return the result as Items and Count format when you are using WebApiAdaptor in SfDataManager. Find the below documentation for your reference. 

Documentation :  

Please let us know if you have any concerns. 

Regards, 
Rahul 



JV Jeff Voigt November 19, 2021 03:08 PM UTC

I don't think the example you provided is working property with VS 2022 .NET 6 (latest) using the Microsoft.AspNetCore.OData (8.0.4)

I've read through the documentation but didn't resolve my issue. I got it working when I manually constructed and created a return type containing a @odata.count property along with a value property. It looks like that is the standard that odata returns. As far as my web api, I wrote code to manually apply the odata options to an iQueryable in order to retrieve the results.

Here is my return type:

public class ODataQueryResponse
{
//required for odatav4
[JsonPropertyName("@odata.count")]
public int Count { get; set; }

//required for syncfusion blazor client ODataV4Adapter
public IEnumerable Value { get; set; }
}

Here is the logic to perform the query as I can't rely on the [EnableQuery] odata attribute like other examples:

[HttpGet("query")]
public Task Query(ODataQueryOptions options)
{
var entitySet = _dbContext.EntityTableInDatabase;
var count = -1;
if (options.Count != null)
{
if (options.Filter != null)
count = options.Filter.ApplyTo(entitySet.AsQueryable(), new ODataQuerySettings()).Count();
else
count = entitySet.AsQueryable().Count();
}

return Ok(new ODataQueryResponse()
{
Count = count,
Value = options.ApplyTo(entitySet.AsQueryable()).ProjectToType().ToList()
});
}

I'm now to the point where I need to figure out how I can inject additional (custom) parameters or post values from the client so I can augment additional filters if possible.

Thanks,

- Jeff



RN Rahul Narayanasamy Syncfusion Team November 23, 2021 03:28 AM UTC

Hi Jeff, 

Thanks for the update. 

We are happy to hear that you have constructed the response data for binding the data. 

Query: I'm now to the point where I need to figure out how I can inject additional (custom) parameters or post values from the client so I can augment additional filters if possible. 

We have validated your query and you want to pass additional parameter to server in order perform the additional operations. You can achieve your requirement by using addParams method of Query class.  

Reference: 

 
<SfGrid TValue="Order" AllowPaging="true" Query=@GridQuery> 
    <GridPageSettings PageSize="10"></GridPageSettings> 
    . . . 
</SfGrid> 
 
@code{ 
    public string ParamValue = "true"; 
    public Query GridQuery { get; set; } 
 
    protected override void OnInitialized() 
    { 
        GridQuery = new Query().AddParams("ej2grid", ParamValue); 
    } 
} 
 

Please let us know if you have any concerns. 

Regards, 
Rahul 


Loader.
Up arrow icon