Home > front end >  While converting ViewBag.JsonData from View in JavaScript, getting The length of the string exceeds
While converting ViewBag.JsonData from View in JavaScript, getting The length of the string exceeds

Time:02-17

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 enter image description here

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

  • Related