Problem with Blazor Grid and WebAPI adapter

Using Dot Net 8 and Syncfusion v 26.2.11


I have the following code:

@page "/test"

@using Syncfusion.Blazor
@using Syncfusion.Blazor.Grids
@using Syncfusion.Blazor.Data

<h3>Test</h3>
<SfGrid TValue="User">
    <SfDataManager Url="api/user" Adaptor="Adaptors.WebApiAdaptor" ></SfDataManager>
    <GridEvents TValue="User" OnActionFailure="@ActionFailure"></GridEvents>
</SfGrid>
@code {
    public void ActionFailure(Syncfusion.Blazor.Grids.FailureEventArgs args)
    {
    }
    public class User
    {
        public int UserId { get; set; }
        public string Username { get; set; }
    }
}

The api/user call returns the following (generated by EF core):

[{"$id":"1","userId":1,"username":"[email protected]"},{"$id":"2","userId":2,"username":"[email protected]"},{"$id":"3","userId":4,"username":"[email protected]"},{"$id":"4","userId":5,"username":"[email protected]"},{"$id":"5","userId":13,"username":"[email protected]"},{"$id":"6","userId":14,"username":"Baanan"}]


I get an error trapped on entry to ActionFailure method:

{"The JSON value could not be converted to System.Collections.Generic.List`1[ProjName.Pages.Test+User]. Path: $.items | LineNumber: 0 | BytePositionInLine: 20."}


I dont understand why I get this error.  Help!


Martin


1 Reply

PS Prathap Senthil Syncfusion Team September 3, 2024 02:52 PM UTC

Hi Martin,


Based on the reported problem we seem that you have used the WebApiAdaptor, but the response you shared is not correct when using the WebApiAdaptor. The response object should contain the properties Items and Count, where Items is the collection of entities, and Count is the total number of entities. It seems that the response in your code is not structured correctly , which is why you are facing this issue. Please ensure that your project returns the correct response. The sample response object should look like this. Kindly refer to the documentation below for more information.


Reference: https://blazor.syncfusion.com/documentation/data/adaptors#web-api-adaptor

{

    "Items": [{..}, {..}, {..}, ...],

    "Count": 830

}

 

 


Additionally, it appears that the property names are in an incorrect format. For example, "$id":"1" is not the correct format. The correct format should be like this: id:1."

Regards,
Prathap Senthil


Loader.
Up arrow icon