asp.net core JsonConvert client or serverside

asp.net core JsonConvert client or serverside

KratosMafiaKratosMafia Posts: 2Questions: 1Answers: 0

So I'm trying to get my code to be server or client side. I'm only pulling about 800 records and some get added to the database. So when loading the information it takes awhile to load it all and I can't seem to find a way to read my json file.

So in my Controller right now I'm just using a ViewBag to send the data to my DataTable to get the information

var list = _context.Tool.Join(_context.Entries, o => o.ToolId, od => od.FktoolId,
               (o, od) => new { o.ToolN, od.Measurement, od.RunDate, od.User, od.V }).ToList();
ViewBag.Testing = JsonConvert.SerializeObject(list);
$('#Table').dataTable({
                "paging": false,
            "info": false,
            "data": @Html.Raw(ViewBag.Testing),
                "columns": [
                    { "data": "Measurement"},
                    { "data": "V" },
                    { "data": "RunDate" },
                    { "data": "User" },
                    { "data": "ToolN" }]
            });

So what I was trying to do was store it into a JSON file and try to pull that information to make it go faster but I don't know if that would work.

// serialize JSON to a string and then write string to a file
System.IO.File.WriteAllText(@"Views\Folder\objects.json", JsonConvert.SerializeObject(list));

I've read over all the documentations and they use php to get information but I'm not using php. I'm pulling a list from my sql server database. I've even copied the "Ajax data source (objects)" on examples and can't even pull that data. I save the objects.txt in my Folder and call it but all it shows on my page is "loading"

$('#Table').dataTable({
             "paging": false,
            "info": false,
                "ajax": "Folder\objects.txt",
                "columns": [
                    { "data": "name"},
                    { "data": "position" },
                    { "data": "office" },
                    { "data": "extn" },
                    { "data": "start_date" }]
            });

Answers

  • KratosMafiaKratosMafia Posts: 2Questions: 1Answers: 0
    edited September 2019

    So I have changed my code to ajax

     $('#Table').dataTable({
                    "responsive": true,
                    "paging": false,
                    "info": false,
                    "ajax": {
                        "url": "/folder/GetList",
                        "type": "POST",
                        "datatype": "json"
                    },
                    "columns": [
                        { "data": "Measurement"},
                        { "data": "V" },
                        { "data": "RunDatee" },
                        { "data": "User" },
                        { "data": "Tool" }]
                });
    
    public JsonResult GetList()
            {
    var list = _context.Tool.Join(_context.Entries, o => o.ToolId, od => od.FktoolId,
                   (o, od) => new { o.ToolN, od.Measurement, od.RunDate, od.User, od.V }).ToList();
    
    return Json(list);
    }
    

    And now I'm getting the error message
    Datatables unable to get property 'length' of undefined or null reference

    and if I add in
    "dataSrc": ""

    into the ajax it returns everything but it is all null. I've put a breakpoint on my code and it's collecting data.

  • colincolin Posts: 15,237Questions: 1Answers: 2,598

    Hi @KratosMafia ,

    Can you post the Ajax response please from that last code snippet, please.

    Cheers,

    Colin

This discussion has been closed.