1

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)">&nbsp;View&nbsp;</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.

adiga
  • 34,372
  • 9
  • 61
  • 83
mr.jm
  • 37
  • 7
  • See [this answer](https://stackoverflow.com/questions/10608198/asp-net-mvc3-returning-success-jsonresult). Also, I suggest using GET to such requests where you’re just (indeed!) getting data from a repository of any sort. – Davide Vitali Mar 25 '19 at 06:45
  • @DavideVitali I used your code but when my query getting big(10k++) records then encounter error. but when my query select like 100 below records the table will populate – mr.jm Mar 25 '19 at 07:06
  • Well, I guess the problem is in the data – Davide Vitali Mar 25 '19 at 07:13
  • if possible then use server side pagination get only 10 or 20 data at time and on next page take another one. – Lalji Dhameliya Mar 25 '19 at 13:08

1 Answers1

0

Here issue is your json string length is exceeds to default because as you convey

expect output will to populate the table with hundred thousand or millions of records.

You can set the MaxJsonLength property in web.config file:

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

set maxJsonLength and default maxJsonLength is 102400 so set this value.

Suggestion: If possible then used server side pagination because getting millions of records from server is not feasible. get only 10 to 20 records at a time and get another records on next page.

Lalji Dhameliya
  • 1,729
  • 1
  • 17
  • 26