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
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 TaskQuery(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
|
<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);
}
} |