im new in coding using javascrip,json and mvc. I have some question hope you can help me. I have codes when i use to debug in controller it have data but when it returning to view using json result it will not interface to table.
Controller/C# code:
[HttpPost]
public ActionResult getAccountability()
{
var data = db.cct_custodials.Where(w => w.EmployeeNo == UserCurrentRole.UserID).OrderBy(o => o.DateCreated).ToList();
var serializer = new JavaScriptSerializer { MaxJsonLength = Int32.MaxValue };
var result = new ContentResult
{
Content = serializer.Serialize(data),
ContentType = "application/json"
};
return result;
}
View/HTML code:
function attachCustodial() {
$.ajax({
beforeSend: function () {
$.blockUI({ baseZ: 2000 });
},
cache: true,
type: 'POST',
url: '/Accountability/getAccountability',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (json) {
var oTable = $('#dtFinalTransfer').dataTable();
oTable.fnDestroy();
oTable.dataTable({
"bSortClasses": false,
"bSort": false,
"bAutoWidth": false,
"bLengthChange": false,
"bFilter": false,
"bPaginate": true,
"sPaginationType":"full_numbers",
"iDisplayLength": 2000,
"aaData": json.result,
"aoColumns": [
{
"mData": null,
"sClass": "ctr valign",
"sWidth": "5%"
},
{
"mData": "ProjectName",
"sClass": "ctr valign",
"sWidth": "15%"
},
{
"mData": "ReferenceNo",
"sClass": "ctr valign",
"sWidth": "15%"
},
{
"mData": "CustodialNo",
"sClass": "ctr valign",
"sWidth": "15%"
},
{
"mData": "IssuedBy",
"sClass": "ctr valign",
"sWidth": "20%"
},
{
"mData": "Name",
"sClass": "ctr valign",
"sWidth": "20%"
},
{
"mData": null,
"sWidth": "10%",
"bSearchable": false,
"sClass": "ctr valign",
"sDefaultContent": '<a class="pointer" onclick="viewCustodial(this)"> View </a>',
}
]
});
tableCounter(oTable);
$.unblockUI();
},
error: function (e) { $.unblockUI(); checkmsg('Please contact your system admininistrator' + e.responseText) }
});
}
but when using this code..I encounter error *"Error during serialization or deserialization using the JSON JavaScriptSerializer.
The length of the string exceeds the value set on the maxJsonLength property."
[HttpPost]
public JsonResult getAccountability()
{
//var data = db.cct_custodials.Where(w => w.EmployeeNo == UserCurrentRole.UserID && w.ReferenceNo == "JAZZ-2017-B0009").OrderBy(o => o.DateCreated).ToList(); for sample
var data = db.cct_custodials.Where(w => w.EmployeeNo == UserCurrentRole.UserID).OrderBy(o => o.DateCreated).ToList();
return Json(new { result = data }, JsonRequestBehavior.AllowGet);
}
expect output will to populate the table with hundred thousand or millions of records.