Articles in this section
Category / Section

Script error throws for exceeding maxJson length while performing serialization or deserialization

2 mins read



While binding very large data to the grid it throws a following exception.

Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.


In our latest MVC grid, we have rendered all the html elements in client side and we have passed the data to the control from server side to client side as Json data by serializing it.

And we have used the default MaxJsonLength value (2097152 characters, which is equivalent to 4 MB of Unicode string data) of the JavaScriptSerializer which is a fixed universal standard length for serializing the data.

We can also use custom serializer to serialize the data Source, this can be done as follows.

Custom datasource serializer class will be as follows.
public class DMSerial : IDataSourceSerializer 
        public string Serialize(object obj)
            var str = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
            return str;
//And the custom serializer can be assigned as follows.
    DataManagerConverter.Serializer = new DMSerial();

Even though custom serializer serialize large amount of data, it will also lead to out of memory exception.


We have provided the following features to overcome the maxJsonLength issue.

1. Load on demand

2. Virtual Scrolling


1. Load on Demand

We have used UrlAdaptor to get the data from the controller through post action and bound the Json result as a grid data source.

Please refer the below code snippet

        .Datasource(ds => { ds.URL("Grid/Page").Adaptor("UrlAdaptor"); })
        .AllowPaging()    /*Paging Enabled*/

Also when we use UrlAdaptor, we need to return the data as JSON and the JSON object must contain field name as “result” with its value as dataSource and one more field name as “count” with its value as dataSource total records count.

public ActionResult Page(DataManager dm)
            DataResult result = new DataResult();
            result.result = DataSource;
            result.count = OrderRepository.GetAllRecord().Count;
            return Json(result, JsonRequestBehavior.AllowGet);
public class DataResult
            public IEnumerable result { get; set; }
            public int count { get; set; }

For paging we have used “PerformSkip” and “PerformTake” method of DataOperation class to get skip and take count of the records.

if (dm.Skip!=0)
           DataSource = obj.PerformSkip(DataSource, dm.Skip);
           if (dm.Take != 0)
           DataSource = obj.PerformTake(DataSource, dm.Take);







2. Virtual Scrolling

        .ScrollSettings(scroll => { scroll.AllowVirtualScrolling().Height(300).Width(0).
VirtualScrollMode(VirtualScrollMode.Normal); })
.Columns(col =>
            col.Field("OrderID").HeaderText("Order ID"). IsPrimaryKey(true). TextAlign(System.Web.UI.WebControls.TextAlign.Right).Width(75).Add();
            col.Field("CustomerID").HeaderText("Customer ID").Width(80).Add();
            col.Field("Freight").HeaderText("Freight"). TextAlign(System.Web.UI.WebControls.TextAlign.Right).Width(75) .Add();
            col.Field("ShipCity").HeaderText("Ship Country").Width(80).Add();
            col.Field("ShipName").HeaderText("Ship Name").Add();  


Please refer the below online demo link for more clarification about Virtual Scrolling feature.

Online Demo Link:!/azure/grid/Paging/VirtualPaging



Did you find this information helpful?
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied