1

I know this is a duplicate question but I tried all the previous answers. Not working for me. So I give here my code. When try to load more than 2000 record I got this error.

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

In Controller

public ActionResult Index(SearchFilter sf)
{
Model m = new Model();
IList<Info> Details;
IList<rViewModel> RLst = new List<rViewModel>();
using (var db = new con())
 {
RLst = (from p in Details
      select new rViewModel
      {
      Id=p.Id,
      Description = p.Description,
      Remark = p.Remark,                               
   }).ToList();
   m.Result = RLst;
}
return View(m);
}

In View,

 $(document).ready(function () {
            $('#Product').dataTable({
                "columnDefs": [
                     { "width": "20%", "targets": 0, "orderable": true, },
                     { "width": "40%", "targets": 1,"orderable": true, },
                     { "width": "40%", "targets": 2,"orderable": true, },                     
                ],"oLanguage": {"sSearch": "Filter:"}
            });
            var t = $('#Product').DataTable();
            t.clear();t.draw();

            var model =@Html.Raw(Json.Encode(Model.Result));
            if(model!=null && model.length>0 )
            {
                AddData(model);
            }
            $('#Id').focus();
        });

Result Model is actually a partial view. On the line in view,

var model =@Html.Raw(Json.Encode(Model.Result));

I got this error. Error image attached belowError image attached here

How to fix?

I tried adding

<system.web.extensions>
       <scripting>
           <webServices>
               <jsonSerialization maxJsonLength="2147483644"/>
           </webServices>
       </scripting>
   </system.web.extensions> 

in web.config already its not working.stuck in this for more than 2 days.. Kindly help..

Rai Vu
  • 1,595
  • 1
  • 20
  • 30
Halim
  • 95
  • 14
  • Refer this thread https://stackoverflow.com/questions/1151987/can-i-set-an-unlimited-length-for-maxjsonlength-in-web-config/7207539#7207539 – Thennarasan Jan 04 '18 at 03:56
  • Why would anybody be serializing or deserializing 2000 records? Even if you fix the issue that you have, the performance will be bad. You probably need to re-think your design. – Racil Hilan Jan 04 '18 at 03:57
  • 1
    This issue seems similar, what you need is setting up `JavaScriptSerializer` with customized `MaxJsonLength`: https://stackoverflow.com/questions/24155468/json-encode-throwing-exception-json-length-exceeded (check out the reason in https://stackoverflow.com/questions/1151987/can-i-set-an-unlimited-length-for-maxjsonlength-in-web-config/7207539#7207539 as well). – Tetsuya Yamamoto Jan 04 '18 at 03:58
  • 1
    Possible duplicate of [Json Encode throwing exception json length exceeded](https://stackoverflow.com/questions/24155468/json-encode-throwing-exception-json-length-exceeded) – Tetsuya Yamamoto Jan 04 '18 at 04:03
  • Possible duplicate of [Can I set an unlimited length for maxJsonLength in web.config?](https://stackoverflow.com/questions/1151987/can-i-set-an-unlimited-length-for-maxjsonlength-in-web-config) – Durga Jan 04 '18 at 08:47

2 Answers2

3

Thank you mr.Tetsuya Yamamoto for your reference. So far I tried all the link. But as per your link I tried the coding below.

In view

@{
    var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    serializer.MaxJsonLength = Int32.MaxValue;
    var jsonModel = serializer.Serialize(Model.Result);
}

and at the line where I got error,I changed my one

var model =@Html.Raw(Json.Encode(Model.Result));

into the line as below:

var model =@Html.Raw(jsonModel);

Previously also I tried these coding, but wrongly entered in controller. From your link only I find out need to put in view. This will help some one whose web.config declaration is not working.

Link Courtesy(once again) as follows: Json Encode throwing exception json length exceeded

Halim
  • 95
  • 14
1

"jsonSerialization maxJsonLength" did not work for me on this problem

But I reference @Halim answer.

This also works for me:

previous code with the problem:

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

code resolution:

<script>
    $(document).ready(function () {
        var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        serializer.MaxJsonLength = Int32.MaxValue;
        var jsonModel = serializer.Serialize(Model);
        modelname(jsonModel)
    });
</script>
Ryan
  • 11
  • 2