Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

201
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda