Editor Not Sending Correct Data Format

Editor Not Sending Correct Data Format

SAnderson1331SAnderson1331 Posts: 2Questions: 1Answers: 0

I have a fully functioning web app using .netframework 4.8 MVC that I'm migrating to .net8 MVC. On the migrated app normal DataTables (AJAX) work fine, but DataTables connected to the Editor API do not work when retrieving data. The controller is sending what looks like correct data, but the View is receiving "{}" as the response. It's my understanding that even if empty (which it isn't) that it should be "{Data:[]}. Even though the GET won't populate the DataTable, the post does seem to add entries to the database. I've tried the current version of Editor as well as the original version of Editor that I was using (2.0.8). I can put a breakpoint on the "return Json(response)" and the view does wait until the controller "continues" before giving the error. The error is "Uncaught TypeError: Cannot read properties of undefined (reading 'length')." I'm at a loss here as I've spent the last 7 hours trying to figure out why it isn't working. Even Claude, GPT 5 and Gemini running through GitHub copilot can't figure out the problem. I did split the GET and POST on the Editor API so that it would return regular JSON and it did populate the DataTable, but the editor functions wouldn't work due to it not seeing the unique identifier. I would prefer to use the standard API implementation though. My implementation is below. Any help will be greatly appreciated!

[Route("api/datatables/currentaddiction/{id}")]
[HttpGet]
[HttpPost]
public IActionResult CurrentAddiction(Guid id)
{
    var connectionString = _configuration.GetConnectionString("DefaultConnection");

    using (var Db = new Database("sqlserver", connectionString))
    {
        var editor = new Editor(Db, "AddictionDisorderCurrent", "ID")
             .Model<AddictionDisorderCurrentViewModel>()
             .Where("GuestId", id);

        var response = editor.Process(HttpContext.Request).Data();
        return Json(response);
    }
}

I've also tried the implementation this way with no success...

[Route("api/datatables/currentaddiction/{id}")]
[HttpGet]
[HttpPost]
public IActionResult CurrentAddiction(Guid id)
{
    var connectionString = _configuration.GetConnectionString("DefaultConnection");

    using (var Db = new Database("sqlserver", connectionString))
    {
        var response = new Editor(Db, "AddictionDisorderCurrent", "ID")
             .Model<AddictionDisorderCurrentViewModel>()
             .Where("GuestId", id)
            .Process(Request).Data();
        return Json(response);
    }
}

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 65,056Questions: 1Answers: 10,772 Site admin
    Answer ✓

    Hi,

    Could you try using the Nettonsoft JSON serialiser, rather than the default .NET one please? TO do that, add the Microsoft.AspNetCore.Mvc.NewtonsoftJson Nuget (if you don't already have it), and then in program.cs:

    // Add services to the container.
    services.AddNewtonsoftJson();
    

    Hopefully that might fix it...

    Allan

  • SAnderson1331SAnderson1331 Posts: 2Questions: 1Answers: 0

    That worked! Thank you!

  • allanallan Posts: 65,056Questions: 1Answers: 10,772 Site admin

    I've just been looking into this a bit more and it is possible to use the default .NET serialiser, but you need to instruct it to include Fields, which it does not do by default.

    For that, in program.cs (or Startup.cs or whatever) you can use:

                services.AddControllers().AddJsonOptions(options =>
                {
                    options.JsonSerializerOptions.IncludeFields = true;
                });
    

    I'm actually going to change the DtResponse class to use properties rather than fields in order to address this and not need any additional setup. There is no effecive difference in useage, so there are not backwards compatilbility concerns.

    I'll make this change for 2.5.

    Thanks for highlighting this issue for me.

    Allan

Sign In or Register to comment.