简体   繁体   中英

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.

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在此处输入图像描述

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM