Razor pages display related data (foreign key) in DataGrid

Hi,

I'm trying to display related data in DataGrid. How do I do that? So far I have this code, but the data does not get loaded into the DataGrid:


public asyncTaskOnPostDataSourceAsync([FromBody]DataManagerRequest dm)
{
varPlt=await _context.Paleta.Include(p => p.Dobavnica).AsNoTracking().ToListAsync();
IEnumerableDataSource=await _context.Paleta
.Include(p => p.Dobavnica)
.ThenInclude(p => p.Dobavitelj)
.AsNoTracking()
.ToListAsync();
DataOperations operation =newDataOperations();
if(dm.Search!=null&& dm.Search.Count>0)
{
DataSource= operation.PerformSearching(DataSource, dm.Search);//Search
}
if(dm.Sorted!=null&& dm.Sorted.Count>0)//Sorting
{
DataSource= operation.PerformSorting(DataSource, dm.Sorted);
}
if(dm.Where!=null&& dm.Where.Count>0)//Filtering
{
DataSource= operation.PerformFiltering(DataSource, dm.Where, dm.Where[0].Operator);
}
int count =DataSource.Cast().Count();
if(dm.Skip!=0)
{
DataSource= operation.PerformSkip(DataSource, dm.Skip);//Paging
}
if(dm.Take!=0)
{
DataSource= operation.PerformTake(DataSource, dm.Take);
}
return dm.RequiresCounts?newJsonResult(new{ result =DataSource, count = count }):newJsonResult(DataSource);
}




If I comment out:

.Include(p => p.Dobavnica).ThenInclude(p => p.Dobavitelj)


Then the data are loaded to the DataGrid.


Attachment: Palete_ab225d54.zip


2 Replies

MI Miha January 9, 2022 03:20 PM UTC

Hi,


I have found the reason for this behavior. The problem was in the object cycle. Because of the way models are built they were cycling. The solution was adding this line to Startup.cs


services.AddMvc().AddJsonOptions(o =>
{
o.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
            })


SK Sujith Kumar Rajkumar Syncfusion Team January 10, 2022 07:36 AM UTC

Hi Miha, 

Greetings from Syncfusion support. 

We are glad to hear that you were able to resolve the problem from your side and thanks for sharing the solution here. 

Please get back to us if you have any further queries. 

Regards, 
Sujith R 


Loader.
Up arrow icon