Alin I
Alin I

Reputation: 590

Setting data row primary ID in TR ID for JQUERY Datatables

I'm using ASP.Net MVC and trying to set the datasource rowID value in each TR #ID attribute.
Problem is I'm getting table id=myDataTable - Requested unknown parameter '1' for row 0.
the controller returns:

var jsonSerialiser = new JavaScriptSerializer();
var json = jsonSerialiser.Serialize(List);
return Json(new
{
    sEcho = param.sEcho,
    iTotalRecords = result.Count,
    iTotalDisplayRecords = 10,
    aaData = json
},
JsonRequestBehavior.AllowGet);

and List is a list of:

public class MyObj
{
    public string DT_RowId {get;set;}
    public string NumeLocatie {get;set;}
}

DataTables initializer is:

    oSesizariTable = $('#myDataTable').dataTable({
        "scrollY": "600px",
        "scrollCollapse": true,
        "scrollX": true,
        "bServerSide": true,
        "sAjaxSource": "ControlSLA/AjaxHandler",
        "bProcessing": true,
        "aoColumnDefs": [
            {
                "width": "30",
                "class": "details-control",
                "orderable": false,
                "mdata": null,
                "defaultContent": "",
                "targets": 0
            },
            { "targets": 1, "width": "30", "mdata": "NumeLocatie" },],
        "fnServerData": function (sSource, aoData, fnCallback) {
            aoData.push({ "name": "DeLa", "value": $('#sesizariDeLa').val() });
            aoData.push({ "name": "La", "value": $('#sesizariLa').val() });
            $.getJSON(sSource, aoData, function (json) {
                fnCallback(json)
            });
        }
    });
    $("#Refresh").click(function (e) {
        oSesizariTable.fnDraw();
    });

I've just started with ASP.Net and MVC... I can't get to the bottom of this!!!
Data sent from controller is:

{ draw = 1, recordsTotal = 2, recordsFiltered = 10, data = "[[\"DT_RowId\":\"ses_35335\",\"NumeLocatie\":\"Galati 1_\"],[\"DT_RowId\":\"ses_35342\",\"NumeLocatie\":\"3 Craiovei\"]]" }

and now the error is:

table id=myDataTable - Requested unknown parameter 'NumeLocatie' for row 0

After I click OK at the error prompt, the table shows every letter in the data in 1 row.

Upvotes: 1

Views: 719

Answers (1)

Alin I
Alin I

Reputation: 590

OK, I've found the problem, and decided to post an answer here, maybe someone else is going to have the same problem. Indeed, as @MarioLopez said (thanks for pointing me in the right direction), there was a problem with the data format:
This

var jsonSerialiser = new JavaScriptSerializer();
var json = jsonSerialiser.Serialize(List);

was enclosing the list of objects in double quotes:

data = "[[\"DT_RowId\"...

when it should've been:

data = [[\"DT_RowId\"...

So I just had to pass the list directly to the JSON response:

return Json(new
{
    sEcho = param.sEcho,
    iTotalRecords = result.Count,
    iTotalDisplayRecords = 10,
    aaData = list // this is the list<MyObj> not the jsonSerialiser
},
JsonRequestBehavior.AllowGet);

Upvotes: 2

Related Questions