For the past year, I have been working with Telerik MVC extensions. When I initially started, it seemed a good idea to use these controls event though I was aware that Telerik no longer supported these controls. Down the line, I came across several instances where I wished I had used some other controls to begin with. Anyway, that is not to say that these controls are not handy. They are very useful if you want to get an application ready with few controls here and there, without having to go into the details.
But sometimes it is just a pure headache. I encountered an issue where I got a “JSON serialization error” when trying to load a large data-set into the telerik mvc grid using Ajax binding. The Data Access layer and the back-end scripts seemed to be populating the right data-set into the model. But when the exception occurred when the model had to be rendered into the telerik mvc grid.
So, let’s create a custom JsonResult class and call it ‘CustomJsonResult’.
Then, we need a factory class that will instantiate and return our CustomJsonResult object.
Now, we need to register this factory class with the Telerik DI Framework in the Application_Start() in the gloabl.asax.
Till here, we have set up a custom JsonResult and a custom JsonSerializer with the max length of json string set to Int32.MaxValue. Now, we will try to load a telerik mv grid with test data using ajax binding and the above JsonSerializer to ensure that we resolved the issue.
We have set up an Event model for test purpose as shown below.
We have a TestController which will populate the model with test data and return a custom JsonResult which will bind to the telerik mvc grid.
In the Index view, we have a telerik mvc grid which binds to the list of events using Ajax binding.
Once we run the project, we observe that we get the grid with the test data and no Json Serialization error!!!