2

I have 300,000+ rows, when I test my project the Datatable is loading for about 30sec then this error occured

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

Error

btw I am using Jquery Datatables, this is my code:

HomeController.cs:

public JsonResult GetAllRecords()
{
    var records = GetRecords().ToList();
    return Json(new { data = records }, JsonRequestBehavior.AllowGet);
}

Javascript:

var FilterSAR = $('#RecordsDatatable').DataTable({
"ajax": {
    "url": '/Home/GetAllRecords',
    "type": "get",
    "datatype": "json"
},
"columns": [
    { "data": "ssn_or_tin", "autoWidth": true },
    { "data": "cusid", "autoWidth": true },
    { "data": "accountNo", "autoWidth": true },
    { "data": "dateTrans", "autoWidth": true },
    { "data": "transCode", "autoWidth": true },
    { "data": "transdescription", "autoWidth": true },
    { "data": "amount", "autoWidth": true },
    { "data": "cashin", "autoWidth": true },
    { "data": "cashout", "autoWidth": true },
    { "data": "source", "autoWidth": true }
]
});

Index.chtml

<table class="table table-hover table-bordered" id="RecordsDatatable">
<thead>
<tr>
    <th>SSN or TIN</th>
    <th>Customer ID</th>
    <th>Account Number</th>
    <th>Date Transaction</th>
    <th>Trans Code</th>
    <th>Trans Description</th>
    <th>Amount</th>
    <th>Cash in</th>
    <th>Cash out</th>
    <th>Source</th>
</tr>
</thead>
</table>

Then i searched for solutions online and i found this code:

public JsonResult GetAllRecords()
{
    var jsonResult = Json(GetRecords().ToList(), JsonRequestBehavior.AllowGet);
    jsonResult.MaxJsonLength = int.MaxValue;
    return jsonResult;
}

Error gone but still not working

The error is gone but my records are still not showing, i think it has something to do with the Json code, I also found other solution like the Serialize but i can't seem to make it work with my code maybe because i am using JsonResult not ContentResult, I'm not sure.

7
  • Your screenshot shows one error, is that a related error? Commented Jul 14, 2017 at 19:11
  • What is that error in your console? Commented Jul 14, 2017 at 19:13
  • @CodingYoshi no, it's not related, i can get rid of that error if i want to, but still my records are not showing even if there are no errors. Commented Jul 14, 2017 at 19:26
  • In the network tab, does the response show any data returning from the server? Commented Jul 14, 2017 at 19:30
  • @CodingYoshi In the screenshot im in the network tab, where can i see the response? Commented Jul 14, 2017 at 19:33

1 Answer 1

5

In HomeController.cs use this

return new JsonResult() { Data = result, JsonRequestBehavior = JsonRequestBehavior.AllowGet, MaxJsonLength = Int32.MaxValue };

instead of

return Json(new { data = records }, JsonRequestBehavior.AllowGet);

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

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.