JSON Return ASP.net Core 3.1

JSON Return ASP.net Core 3.1

titechnotitechno Posts: 9Questions: 3Answers: 0
edited October 21 in Free community support

Hi,
I have a Problem with the display of the editor datatable.
Maybe someone got a similiar problem and can help me.

Like mentioned in the title I try to use the editor in an ASP.net Core 3.1 (C#) project.

When returning the respone as a Json like this:

return  Json( new { data = response.data.ToList() });

The table gets displayed and I can edit the content of the table no problem.

When returning the response like this (similar to the examples and the manual):

return Json(response);

The table won't even load.
The Problem is when returning the Json like shown above the object gets of course cut and the errors are not send to the frontend. So validation does not work.

In the console it puts out the following error:

TypeError: Cannot read properties of undefined (reading 'length')
at https://cdn.datatables.net/1.11.0/js/jquery.dataTables.min.js:62:341
at Object.f [as success] (https://cdn.datatables.net/1.11.0/js/jquery.dataTables.min.js:48:499)
at fire (https://code.jquery.com/jquery-3.5.1.js:3496:31)
at Object.fireWith [as resolveWith] (https://code.jquery.com/jquery-3.5.1.js:3626:7)
at done (https://code.jquery.com/jquery-3.5.1.js:9786:14)
at XMLHttpRequest.<anonymous> (https://code.jquery.com/jquery-3.5.1.js:10047:9)

Here is the JavaScript part of the table:

var editor;

$(document).ready(function () {
    loadDataTable();
});

function loadDataTable() {
    editor = new $.fn.dataTable.Editor({
        ajax: "/lieferanten/lieferantenUmsaetze",
        table: "#DT_load",
        fields: [{
            label: "Lief. Nummer:",
            name: "liefNr"
        }, {
            label: "Ges. Nummer:",
            name: "gesNr"
        }, {
            label: "Ah. Nummer:",
            name: "ahNr"
        }, {
            label: "Umsatz:",
            name: "umsatz"
        }, {
            label: "Jahr:",
            name: "jahr"
        }
        ]
    });


    var table = $('#DT_load').DataTable({
        dom: "Bfrtip",
        ajax: "/lieferanten/lieferantenUmsaetze",
        order: [[1, 'asc']],
        columns: [
            {
                data: "liefNr",
                orderable: false },
            { data: "gesNr" },
            {
                data: "ahNr",
                orderable: false
            },
            {
                data: "jahr",
                orderable: false
            },
            {
                data: "umsatz",
                render: $.fn.dataTable.render.number('.', ',', 2,'', ' €'),
                orderable: false
            }
        ],
        keys: {
            columns: ':last-child',
            editor: editor
        },
    });
}

Answers

  • titechnotitechno Posts: 9Questions: 3Answers: 0

    Here the working JSON String:

    "{\"ContentType\":null,\"SerializerSettings\":null,\"StatusCode\":null,\"Value\":{\"data\":[{\"DT_RowId\":\"row_1\",\"liefNr\":\"1232\",\"gesNr\":\"510\",\"ahNr\":\"000\",\"jahr\":\"2020\",\"umsatz\":23234.59,\"id\":1},{\"DT_RowId\":\"row_2\",\"liefNr\":\"1232\",\"gesNr\":\"123\",\"ahNr\":\"210\",\"jahr\":\"2020\",\"umsatz\":4565.67,\"id\":2},{\"DT_RowId\":\"row_4\",\"liefNr\":\"1232\",\"gesNr\":\"236\",\"ahNr\":\"651\",\"jahr\":\"2020\",\"umsatz\":123456.00,\"id\":4},{\"DT_RowId\":\"row_6\",\"liefNr\":\"1232\",\"gesNr\":\"124\",\"ahNr\":\"123\",\"jahr\":\"2020\",\"umsatz\":12365.50,\"id\":6},{\"DT_RowId\":\"row_7\",\"liefNr\":\"1232\",\"gesNr\":\"777\",\"ahNr\":\"123\",\"jahr\":\"2020\",\"umsatz\":156442.00,\"id\":7},{\"DT_RowId\":\"row_9\",\"liefNr\":\"2154\",\"gesNr\":\"610\",\"ahNr\":\"\",\"jahr\":\"2020\",\"umsatz\":123456.00,\"id\":9},{\"DT_RowId\":\"row_11\",\"liefNr\":\"1232\",\"gesNr\":\"999\",\"ahNr\":\"999\",\"jahr\":\"2020\",\"umsatz\":200.13,\"id\":11}]}}"

    And the not working JSON String:

    "{\"ContentType\":null,\"SerializerSettings\":null,\"StatusCode\":null,\"Value\":{\"draw\":null,\"data\":[{\"DT_RowId\":\"row_1\",\"liefNr\":\"1232\",\"gesNr\":\"510\",\"ahNr\":\"000\",\"jahr\":\"2020\",\"umsatz\":23234.59,\"id\":1},{\"DT_RowId\":\"row_2\",\"liefNr\":\"1232\",\"gesNr\":\"123\",\"ahNr\":\"210\",\"jahr\":\"2020\",\"umsatz\":4565.67,\"id\":2},{\"DT_RowId\":\"row_4\",\"liefNr\":\"1232\",\"gesNr\":\"236\",\"ahNr\":\"651\",\"jahr\":\"2020\",\"umsatz\":123456.00,\"id\":4},{\"DT_RowId\":\"row_6\",\"liefNr\":\"1232\",\"gesNr\":\"124\",\"ahNr\":\"123\",\"jahr\":\"2020\",\"umsatz\":12365.50,\"id\":6},{\"DT_RowId\":\"row_7\",\"liefNr\":\"1232\",\"gesNr\":\"777\",\"ahNr\":\"123\",\"jahr\":\"2020\",\"umsatz\":156442.00,\"id\":7},{\"DT_RowId\":\"row_9\",\"liefNr\":\"2154\",\"gesNr\":\"610\",\"ahNr\":\"\",\"jahr\":\"2020\",\"umsatz\":123456.00,\"id\":9},{\"DT_RowId\":\"row_11\",\"liefNr\":\"1232\",\"gesNr\":\"999\",\"ahNr\":\"999\",\"jahr\":\"2020\",\"umsatz\":200.13,\"id\":11}],\"recordsTotal\":null,\"recordsFiltered\":null,\"error\":null,\"fieldErrors\":[],\"id\":null,\"meta\":{},\"options\":{},\"searchPanes\":null,\"files\":{},\"upload\":{\"id\":null},\"debug\":null,\"cancelled\":[]}}"

  • titechnotitechno Posts: 9Questions: 3Answers: 0

    The Debugger Code is: umugey
    Help would be realy appreciated!

  • kthorngrenkthorngren Posts: 14,171Questions: 25Answers: 3,343

    See my answer in your other thread.

    Kevin

Sign In or Register to comment.