Grid property display issues in .NET Core 6.0 application (suspect camel case problem - but JSON Serialization tips don't work!)

1 Answer 793 Views
Grid
Paul
Top achievements
Rank 1
Paul asked on 24 Mar 2022, 05:36 PM

Hi everyone.

I've got a grid on my razor page populated from a list of API-sourced records and none of the fields are displaying on there.  I'm using local binding direct from an incoming model property...

@(
Html.Kendo().Grid(Model.AllAccounts)
.Name("Grid")
.Columns(columns =>
{
columns.Bound(b => b.AccountId).Visible(false);
columns.Bound(b => b.AccountDescription).Title(Model.AccountDescriptionLabel);
columns.Bound(b => b.AccountHolder).Title(Model.AccountHolderLabel);
columns.Bound(b => b.ContractsCount  ).Title("Total Contract(s)");
columns.Bound(b => b.ContactsCount).Title("Total Contact(s)");
columns.Bound(b => b.UsersCount).Title("Total User(s)");
columns.Command(command => command.Custom(Model.AccountDetailsButtonText).Click("showDetails"));
})
.Pageable()
.Sortable()
.Scrollable(scr=>scr.Height(430))
.Filterable()
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(20)
.ServerOperation(false)
.Events(events => events.Error("error_handler")
)
)
)


If I look at the records in debug then everything is populated, but the fields aren't showing on the grid row (Empty Grid Row.jpg).

I've got a button on the row linked to the record and if I intercept the dataitem in the script you can see that the camel case fields are populated but the pascal case ones aren't (which is definitely not the case when viewing records in debug!) (Data Item Grab.jpg).

I've followed the first two options in the guidance about the camel case issue from the JSON Serialisation Page but nothing is working for me (snippet of Program.cs follows).

// Add services to the container.
services.AddControllersWithViews()
    .AddJsonOptions(options =>
                options.JsonSerializerOptions.PropertyNamingPolicy = null);

Can anyone advise me what to do here? I've lost most of today on this issue.

Thanks for your time!

 

1 Answer, 1 is accepted

Sort by
0
Aleksandar
Telerik team
answered on 25 Mar 2022, 11:33 AM

Hello Paul,

You mention you use RazorPages, yet you call the AddControllersWithViews() method and set the JSON serialization settings to it. Note that there is a difference between the AddController, AddMvc, AddControllersWithViews, AddRazorPages methods and each of them adds different features to the ASP.NET Core application. Read more on this here.

To set the JSON serialization options for RazorPages you can try the following, for example:

// Add services to the container.
builder.Services.AddRazorPages()
                // Maintain property names during serialization. See:
                // https://github.com/aspnet/Announcements/issues/194
                .AddNewtonsoftJson(options => options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver());

Regards,
Aleksandar
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Grid
Asked by
Paul
Top achievements
Rank 1
Answers by
Aleksandar
Telerik team
Share this question
or