Columns Missing Data

Columns Missing Data

loganfgloganfg Posts: 5Questions: 3Answers: 0
edited July 2016 in Free community support

I'm having trouble outputting the data in my table after the ajax call. When my page loads, the data table is created with the correct format, however the data that should be contained inside the table is not being displayed. Instead all I'm getting is the "Loading..." text inside the empty table. The first chunk of code is my javascript for creating the table, the second is the C# code used to obtain the data and return it as a JsonResult, and last but not least is the model being used by the server to collect the data from the data table into an array. How do I alter the 'columns' option to make it work correctly?

$('#SP_Table').DataTable({
        autoWidth: false,
        lengthChange: false,
        info: false,

        responsive: true,

        //processing: true,
        //serverSide: true,
        //deferRender: true,

        searching: true,

        ordering: true,
        order: [0, 'asc'],

        paging: true,
        pagingType: "full_numbers",
        pageLength: SP_NumberOfRows($(window).height() - 330),

        ajax: {
            url: SP_Table_Data_URL,
            type: 'GET',
            cache: false,
            async: false,
            dataType: 'json'
        },

        columns: [
            { title: "Username", data: "Username", className: "alignleft", width: "88%" },
            { title: "", data: "readbutton", className: "aligncenter", orderable: false, searchable: false, width: "4%" },
            { title: "", data: "editbutton", className: "aligncenter", orderable: false, searchable: false, width: "3%" },
            { title: "", data: "deletebutton", className: "aligncenter", orderable: false, searchable: false, width: "5%" }
        ],

        initComplete: function () {

        }
    });
[HttpGet]
        public JsonResult SecurityPermissionsTableData()
        {
            List<SecurityPermissionsGridModel> list = securityPermissionsTable.Get(System.Security.Principal.WindowsIdentity.GetCurrent().Name.Split('\\').Last());

            var json = JsonConvert.SerializeObject(list);

            return ResultJson(json);
        }
public SecurityPermissionsGridModel()
{
     this.Username = null;
     this.readbutton = null;
     this.editbutton = null;
     this.deletebutton = null;
}

Note: an example of the data returned by the ajax call is:

Text:

"[ \"data\" : { \"Username\" : \"loganfg\", \"readbutton\" : \"<a onclick='SP_read(\"7\")' class='SRKbutton tiny SP_rbutton'>Details</a>\", \"editbutton\" : null, \"deletebutton\" : null}]"

JSON visualizer:
Username: "loganfg"
readbutton: "a onclick='SP_read("7")' class='SRKbutton tiny SP_rbutton'>Details</a>"
editbutton: ""
deletebutton: ""

Answers

  • allanallan Posts: 61,665Questions: 1Answers: 10,095 Site admin

    It looks like the reply from the server is a string rather than a JSON object (its a string that contains a JSON object!).

    The best way would be to modify the C# code to return just plain JSON rather than a string. I'm afraid I can't remember off the top of my head what the correct way to do that is, I think its an attribute on the controller (its annoying that ResultJson gets enclosed as a string at all!).

    If you can't find a way to return just plain JSON you could probably add:

    dataSrc: function ( str ) {
      return JSON.parse( str ).data;
    }
    

    to your ajax object.

    But as I say, I think you would be much better served getting the server to just return JSON.

    Allan

  • loganfgloganfg Posts: 5Questions: 3Answers: 0

    When you say plain JSON do you mean of the form:

    [
    "data" : {
    "Username" : "loganfg",
    "readbutton" : null,
    "editbutton" : null,
    "deletebutton" : null
    }
    ]

    or should the following suffice:

    return Json(list, JsonRequestBehavior.AllowGet);

    which returns it more similar to an object.

    Currently I can't find any way of returning the first from my controller, and the second isn't working.

  • allanallan Posts: 61,665Questions: 1Answers: 10,095 Site admin

    Yes - at the moment you will see that the very first character being returned is a double quote - that basically makes everything else a string. Technically that is valid JSON, but it is fairly useless JSON since it isn't an object, but rather just a single string value.

    This is the first thread I found for ".net return json not string". It looks like you might just need to send/set the content type as appropriate.

    Allan

This discussion has been closed.