最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ASP.NET MVC Error during serialization or deserialization using the JSON JavaScriptSerializer. exceeds the value se

programmeradmin1浏览0评论

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.

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;
}

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.

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.

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;
}

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.

Share Improve this question asked Jul 14, 2017 at 19:06 HansmagzHansmagz 1033 silver badges11 bronze badges 7
  • Your screenshot shows one error, is that a related error? – CodingYoshi Commented Jul 14, 2017 at 19:11
  • What is that error in your console? – Travis Acton 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. – Hansmagz Commented Jul 14, 2017 at 19:26
  • In the network tab, does the response show any data returning from the server? – CodingYoshi Commented Jul 14, 2017 at 19:30
  • @CodingYoshi In the screenshot im in the network tab, where can i see the response? – Hansmagz Commented Jul 14, 2017 at 19:33
 |  Show 2 more ments

1 Answer 1

Reset to default 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);

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论