I am using the new System.Text.Json in asp.net core and I noticed that the grid does not bind to the model when using the new .net Core Json library. If I use Newtonsoft.Json the bind binding works fine.
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(IISDefaults.AuthenticationScheme);
services.AddKendo();
services.AddControllersWithViews();
//.AddNewtonsoftJson(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
}
@(Html.Kendo().Grid<TestModel>()
.Name("grid")
.Columns(columns =>
{
columns.Bound(e => e.email);
columns.Bound(e => e.username);
})
.HtmlAttributes(new { style = "height:430px;" })
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(100)
.Read(read => read.Action("Read", "home"))
)
)
Model
public class TestModel
{
public string archiveGuid { get; set; }
public string orgUid { get; set; }
public string orgName { get; set; }
public string orgExtRef { get; set; }
public string userUid { get; set; }
public string username { get; set; }
public string email { get; set; }
}
Here is what is returned with NewtonSoft JSON. I also noticed that D in the data response sent is capitalized when using Newtonsoft.json. Any ideas? It could be due to the contract resolver not being set when we are using the System.Text.Json as posted here https://www.telerik.com/forums/grid---binding-doesn't-work but I don't think there is a contract resolver option for the System.Text.Json.
16 Answers, 1 is accepted

Hello Syed,
The issue that you have observed is exactly the one discussed in the forum thread in question. If you would like to skip the Newtonsoft.Json in your project, you should define a Custom() DataSource with a schema. Just like Viktor has suggested in his latest post:
https://www.telerik.com/forums/grid---binding-doesn't-work#nvbEGC71DE6jUybCHyH_Bg
Regards,
Veselin Tsvetanov
Progress Telerik

Thanks Veselin. Is this information documented somewhere? I found a reference to this on stackoverflow but the talking point 4 does not mention setting up serialization option for JSON
Hello Syed,
The ToDataSourceResult() call will return a Kendo.Mvc.UI.DataSourceResult which have all its properties capitalized. As a result, the DataSource utility on the client would expect the Data member to be with a capital letter.
You are correct that the Serialization options configuration is not present in our Getting started articles and that is something we should take care of. By the way, as you will be using the System.Text.Json namespace, here is how to configure it, so that it does not change the casing of the properties:
services
.AddMvc(options => options.EnableEndpointRouting = false)
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
.AddJsonOptions(options => options.JsonSerializerOptions.PropertyNamingPolicy = null);
The above will allow you to avoid the Custom DataSource definition, as it will keep the leading capital letters.
Regards,
Veselin Tsvetanov
Progress Telerik


For anyone else like me who was trying to use this in Razor Pages / Controllers, the fix for json casing provided by Veselin will look more like this...it brought my grid to life anyway:
services.AddRazorPages();
services.AddControllers()
.AddJsonOptions(options => options.JsonSerializerOptions.PropertyNamingPolicy = null);
services.AddKendo();
Hi Andrew,
We will consider the implementation of a modified DataSource type that would respect the .Net Core default serialization configuration. That is, however, currently not scheduled for implementation. What needs to be done in that case is to modify the Schema configuration to use camalCase properties instead of PascalCase.
Regards,
Veselin Tsvetanov
Progress Telerik

Hello Andrew,
I am referring to the DataSource Schema configuration which will need to be automatically generated when the DataSource is defined within an HTML helper with a defined model like the following:
@(Html.Kendo().Grid<TestModel>()
When receiving item properties with lower case, that schema should properly map the camelCase field to the PascalCase property from the model. To do that we will need to implement a new type of DataSource, that will make the above conversion automatically.
Regards,
Veselin Tsvetanov
Progress Telerik

How about a blog post about what will be recommended going forward in the future? Should we just continue to use NewtonSoft.Json or should we use the new Microsoft version of Json? Should we have to worry about things breaking in the data grid? I have already lost a considerable amount of wasted time before I realized that the problem I had with my broken Telerik Grids was my failure to address the JSON problem, and differences between NewtonSoft and Microsoft when migrating from ASP.NET Core 2.2 to Core 3.1.
Since Telerik always advertises that they make things easy for developers, why not support both bindings out-of-the-box for the Telerik Data Grid control? Then you could just add a property switch for NewtonSoft JSON versus Microsoft JSON. Or alternatively, a property corresponding to an Enum with at least 3 different options: NewtonSoft, Microsoft and Custom.
Thanks for considering this suggestion.
Hi Carl,
Your idea to create a resource explaining how the System.Text.Json could be configured to substitute the Newtonsoft.Json in a .Net Core 3.0 project is indeed a very good suggestion. We will include that task in our implementation queue and will create that resource as soon as our priorities allow that.
As per the suggested property switch in the Grid (or better its DataSource), that would be something similar to what we have discussed with Andrew. Note that the default behavior of changing the beginning letter of the fields does not differ in System.Text.Json compared to Newtonsoft.Json. The default serialization in .Net Core applications (since version 1.0) has always been changing PascalCase fields to camelCase. Therefore, concerning the data that is being transferred between the server and the client, there are no changes in the default format. What has changed, and needs to be explained, is the change in the default Json resolver. In consequence, when the Sytem.Text.Json is used, .Net Core 3.0 projects require a different configuration compared to previous .Net Core versions.
Regards,
Veselin Tsvetanov
Progress Telerik

What is latest update from Telerik on status of use of NewtonSoft.Json versus Microsoft System.Text.Json?
Microsoft has documentation at
https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to
But what is Telerik's position vis-a-vis use of JSON libraries by Telerik UI for Net Core? And for Net 5 compatible UI libraries for use when Net 5 is released next November?
Has Telerik prepared a blog post discussing these questions?
Hello Carl,
We have included the following documentation article on the serialization topic:
https://docs.telerik.com/aspnet-core/compatibility/json-serialization
Since .Net Core 3 and the release of the System.Text.Json library Newtonsoft.Json is no longer needed in projects using Telerik UI for ASP.NET Core.
Regards,
Veselin Tsvetanov
Progress Telerik
Our thoughts here at Progress are with those affected by the outbreak.

Hi Marc,
In order to preserve the casing of the property names, you should configure the contract resolver/property naming policy used by the application. In .Net Core 3.0 that would be:
// Add framework services.
services
.AddControllersWithViews()
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
// Maintain property names during serialization. See:
// https://github.com/aspnet/Announcements/issues/194
.AddNewtonsoftJson(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
While in .Net Core 3.1:
services.AddMvc(s => s.EnableEndpointRouting = false)
.AddJsonOptions(o=> { o.JsonSerializerOptions.PropertyNamingPolicy = null; });
Then you could use the following endpoint for the Grid Read action, which will keep the casing of the properties in the items:
public ActionResult Orders_Read([DataSourceRequest]DataSourceRequest request)
{
DataSourceResult result = orders.ToDataSourceResult(request);
return Json(result);
}
Regards,
Veselin Tsvetanov
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/.
