This is a migrated thread and some comments may be shown as answers.

Json Serialization Breaks Grid

10 Answers 1148 Views
Grid
This is a migrated thread and some comments may be shown as answers.
AP
Top achievements
Rank 1
Iron
Iron
Veteran
AP asked on 04 Oct 2016, 10:09 AM

I've just set up a grid using ASP.NET core, and the controller method to return data is called, but no data is displayed by the grid. Looking at the JSON returned  in Fiddler, it seems the field name cases have been changed by the serializer to be camel case, with the first letter always being lower case.

 

As the field names of the table are uppercase, this means they aren't being picked up by the grid. The only other option is to serialize as snake case, which doesn't help.

How can this be handled?

10 Answers, 1 is accepted

Sort by
0
AP
Top achievements
Rank 1
Iron
Iron
Veteran
answered on 04 Oct 2016, 10:25 AM

After a bit of searching I found a post explaining how to turn this behaviour off.

Add this to the Startup.cs file:-

services.AddMvc()
              .AddJsonOptions(opt =>
              {
                  var resolver = opt.SerializerSettings.ContractResolver;
                  if (resolver != null)
                  {
                      var res = resolver as DefaultContractResolver;
                      res.NamingStrategy = null// <<!-- this removes the camelcasing
                  }
              });

0
Viktor Tachev
Telerik team
answered on 06 Oct 2016, 11:28 AM
Hi,

Indeed the properties are serialized in camel case to the client by default. This was a breaking change that was introduced before the final release of MVC Core.


In order to revert the behavior you need to modify the settings in the ConfigureServices method as you have done. This is also described in the documentation. Please note Step 4 in the article below:



Regards,
Viktor Tachev
Telerik by Progress
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
AP
Top achievements
Rank 1
Iron
Iron
Veteran
answered on 06 Oct 2016, 11:47 AM
OK. I missed that, as the explanation just says to add services.AddKendo() at the end. The documentation could do with explaining the other entry, and what it does. 
0
Viktor Tachev
Telerik team
answered on 07 Oct 2016, 02:20 PM
Hi,

Thank you for the feedback. Currently the issue is shown only in the comments in the code snippet. However, we will consider updating the description in a way that it is more clear what is the purpose of the added code.


Regards,
Viktor Tachev
Telerik by Progress
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Francis
Top achievements
Rank 1
answered on 13 Oct 2016, 07:07 PM
Thanks AP. Digging my head as to why the grid was empty for a while.
0
Bradley
Top achievements
Rank 1
answered on 04 Aug 2017, 01:50 AM
Do you guys plan to fix this to use the new "default" since this is the CORE version I would expect this to work "out of the box" plus for those of us that already have applications we cant just change out the default serialize since it will have breaking changes for us.
0
Bradley
Top achievements
Rank 1
answered on 04 Aug 2017, 02:27 AM

I was able to find a "fix" but the default should still accept camelCase as the default.  But incase anyone runs in to this and cant convert their entire app over.  Just set the .From property on the field in the Model.

 .Model(m =>
              {
                  m.Id(x => x.Id);
                  m.Field(x => x.Name).From("name");
                  m.Field(x => x.Surname).From("surname");
                  m.Field(x => x.IsActive).From("isActive");
              }))

 

0
Bradley
Top achievements
Rank 1
answered on 04 Aug 2017, 02:29 AM

Here is a work around if people need since telerik didn't offer it up by default for some reason.  I would still expect the camelCase to be the default though.

 

.Model(m =>
             {
                 m.Id(x => x.Id);
                 m.Field(x => x.Name).From("name");
                 m.Field(x => x.Surname).From("surname");
                 m.Field(x => x.IsActive).From("isActive");
             }))
0
Emanuel
Top achievements
Rank 1
answered on 27 Mar 2019, 02:25 PM

This workaround is not working for me. (the one with From)

How can I specify the correct names?

I cannot change the JSON serializer, will impact all the application.

0
Viktor Tachev
Telerik team
answered on 01 Apr 2019, 11:13 AM
Hello Emanuel,

The From option in the Model definition can be used when DataSource is specified as Custom. The example below shows how custom DataSource can be used with the Grid widget.




Regards,
Viktor Tachev
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
AP
Top achievements
Rank 1
Iron
Iron
Veteran
Answers by
AP
Top achievements
Rank 1
Iron
Iron
Veteran
Viktor Tachev
Telerik team
Francis
Top achievements
Rank 1
Bradley
Top achievements
Rank 1
Emanuel
Top achievements
Rank 1
Share this question
or