Using DataTables-Editor-Server with DotNet Core 3.1

Using DataTables-Editor-Server with DotNet Core 3.1

parcivalparcival Posts: 28Questions: 8Answers: 0

For anyone that may run into this: I was upgrading my project that uses the Editor Server DLL to handle the data feed for DataTables from DotNet Core 2.2 to 3.1. After the upgrade I was not able to get any data from the ActionResult even though the DLL itself was retrieving data.

The solution was to add a Newtonsoft Json handler and configuring it in startup.cs:
First add the NuGet package:

Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson

Then add this to startup.cs

services.AddControllersWithViews()
    .AddNewtonsoftJson(o =>
        o.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize);

I hope this helps someone :smile:

Replies

  • parcivalparcival Posts: 28Questions: 8Answers: 0

    Here are the full steps to recreate using an MSSQL database:

    1. Load the sample database from https://editor.datatables.net/manual/net/installing into a database called "Datatable"
    2. Create a new project in Visual Studio 2019
    3. ASP.NET Core Web Application
    4. Web Application (Model-View-Controller)

    Once project is setup, install the following NuGet packages using the Package Manager Console:
    - Install-Package DataTables-Editor-Server
    - Install-Package System.Data.SqlClient
    - Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson

    Edit Program.cs to add this line before CreateHostBuilder in the Main function

                DbProviderFactories.RegisterFactory("System.Data.SqlClient", SqlClientFactory.Instance);
    

    Add a using for the Data.SqlClient and Data.Common

    using System.Data.SqlClient;
    using System.Data.Common;
    

    Edit Startup.cs to modify the line that configures the AddControllersWithViews service

                services.AddControllersWithViews()
                    .AddNewtonsoftJson(o =>
                    o.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize);
    

    Also add a using for Datatables

    using DataTables;
    

    Finally, add a new ActionResult to the Home controller:

    public ActionResult DataTables()
    {
        var dbConnection = @"server=(LocalDb)\MSSQLLocalDB; database=Datatable;Integrated Security=SSPI;";
        using (var db = new Database("sqlserver", dbConnection))
        {
            var response = new Editor(db, "sites")
                .Field(new Field("id"))
                .Field(new Field("name"))
                .Process(Request)
                .Data();
    
            return Json(response);
        }
    }
    
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Many thanks for posting this! Our newer example projects have this, for exactly this reason, but you are right, anyone upgrading from .NET Core 2 or before to 3 will need to take similar steps due to how the JSON libraries are now referenced.

    Regards,
    Allan

This discussion has been closed.