How to restrict OData API call multiple times in Blazor DataGrid?

Answer:

The ForeignKeyColumn will refresh while loading ForeignKey data asynchronously. While refreshing data from foreign key columns, the DataGrid will also be refreshed. So the request will be sent to the OData Controller for the second time. It's the default behavior. To avoid multiple API calls, load foreign key data in the Initialized Lifecycle. Here's the code snippet for your reference.


protected override void OnInitialized()

{

LoadProviders(); // load foreign key data here

}

protected override async Task OnAfterRenderAsync(bool firstRender)

{

if (firstRender)

{

await Initialize();

}

}

public async Task Initialize()

{

await Task.Delay(10);

this.IsInitialRender = true;

//LoadProviders(); -- remove loading data here.

UpdateIdQueryData();

}



Loader.
Up arrow icon