Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
While converting ViewBag.JsonData from View in JavaScript, getting The length of the string exceeds the value set on the maxJsonLength property

I'm working on a project where I've to pass a huge list of data (500 000 records or more) through ViewBag. From my controller, I'm sending data shown as below.

public ActionResult Index()
{
  var list = new List<DropdownList>();
    
  for (int i = 0; i < 500000; i++)
  {
     list.Add(new DropdownList() { id = i, text = "Test Data-" + i });
  }
  ViewBag.JsonData = JsonConvert.SerializeObject(list);
  return View();
}

And from my view, I'm using the following code to parse that data in JavaScript. While trying this code getting the maxJsonLength exceeds the error.

<script>
$(document).ready(function(){
   @Html.Raw(Json.Encode(@ViewBag.JsonData));
});
</script>

I also tried this & It's working with 500k records while data does not contain special characters. If data contain some special character then it's not working.

<script>
$(document).ready(function(){
   JSON.parse('@ViewBag.JsonData');
});
</script>

Note: I'm not getting error in .cs (c# code) file cause this solution already implemented in my project.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You are getting the error of MaxJsonLength overflow issues, This isn't a JavaScript issue but issues on C#. It doesn't support a large number of data sets in Json.Encode.

It's possible to tackle this situation only this way of a JavascriptSerializer and setting the MaxJsonLength property manually or only used by Newtonsoft.Json

C# Codes

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var list = new List<DropdownList>();

        for (int i = 0; i < 2000000; i++)
        {
            list.Add(new DropdownList() { id = i, text = "Test Data-" + i });
        }            
        
        //First solutions with JavaScriptSerializer
        var serializer = new System.Web.Script.Serialization.JavaScriptSerializer() { MaxJsonLength = Int32.MaxValue };
        ViewBag.DataAsList = serializer.Serialize(list);

        //Second Solutions with Newtonsoft.Json
        ViewBag.DataAsList = JsonConvert.SerializeObject(list);

        return View();
    }
}

Razor Pages

 <script>
        $(document).ready(function () {
            function mockDataForDataAsList() {
                return @Html.Raw(ViewBag.DataAsList);
            }  
            console.log(mockDataForDataAsList());
        });
  </script>

Console Log enter image description here

In this scenario, you can handle 2 million records within a few seconds. Hope you got your answers!

about 4 years ago · Juan Pablo Isaza Report

0

You are getting the error in "C# code". This is not a JavaScript issue.

If editing web.config doesn't help, you can try using an instance of a JavascriptSerializer and setting the MaxJsonLength property manually:

@{
   var serializer = new JavaScriptSerializer() { MaxJsonLength = Int32.MaxValue };
    
   var jsonData = serializer.Serialize(@ViewBag.JsonData);
}

See more here and here.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!