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 [email protected](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 [email protected](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..

5

2 Answers 2

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 [email protected](Json.Encode(Model.Result));

into the line as below:

var model [email protected](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

Sign up to request clarification or add additional context in comments.

Comments

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.