New added row not showing (instead, showing first record twice)

New added row not showing (instead, showing first record twice)

fchopofchopo Posts: 4Questions: 1Answers: 0

Hi,
I am using Editor version 1.4.2 and DataTables 1.10.7 with ASPNET WebApi2 project and SQL Server 2014. I am using the same sample posted here http://editor.datatables.net/manual/net/webapi. Update and Delete actions work fine, but Create one does not. It creates the record in the table, but the server data always returns the first row of the table, not the new one, as it should be and it is explained here: http://editor.datatables.net/manual/server Therefore it shows the first record of the table twice, plus all other records (except the new one). When I refresh the page, the records are shown correctly.

Any clue about what could be the problem?

Thank you very much in advance.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,692Questions: 1Answers: 10,500 Site admin

    Hi,

    Thanks for your question. A couple of questions in return:

    • What is your SQL server type (SQL Server, MySQL, etc)?
    • Does your SQL table have a primary key that is auto incrementing?
    • Do you submit a value for the primary key (generally the answer to this should be no)?

    Thanks,
    Allan

  • fchopofchopo Posts: 4Questions: 1Answers: 0

    Hi Allan,
    Answering your questions:
    - SQL Server 2014 Standard Edition.
    - Yes, the table has a primary key ID Field that is autoincrementing.
    - No, I don't show the ID Field in the table and do not submit it.
    Thank you very much for your assistance.
    Ferran.

  • allanallan Posts: 63,692Questions: 1Answers: 10,500 Site admin

    Very odd! Could you possibly show me your controller, model and the Javascript you are using please? I haven't come across this one before I'm afraid, so I don't have an immediate answer for you.

    Allan

  • fchopofchopo Posts: 4Questions: 1Answers: 0
    edited July 2015

    Model:

    public class StudentModel
        {
            public string Name { get; set; }
        }
    

    Controller:

           [Route("api/Student")]
            [HttpGet]
            [HttpPost]
            
            public IHttpActionResult Student()
            {
                var request = System.Web.HttpContext.Current.Request;
    
                using (var db = new Database("sqlserver", "Server=fcg-portatil; initial catalog=datatables; Integrated Security=SSPI"))
                {
                    var response = new Editor(db, "dbo.Students","StudentID")
                        .Model<Models.StudentModel>()
                        .Field(new Field("Name").Validator(Validation.NotEmpty()))
                        .Process(request)
                        .Data();
    
                    return Json(response);
                }
            };
    

    Javascript:

        var editor;
        $(document).ready(function () {
    
            editor = new $.fn.dataTable.Editor({
                ajax: "/api/Student",
                table: "#tblCentre",
                fields: [{label: "Student name", name: "Name"}]
            });
    
            var table = $("#tblStudent").dataTable({
                lengthChange: false,
                ajax: "/api/Student",
                columns: [{ data: "Name" }]
            });
    
    
            var tableTools = new $.fn.dataTable.TableTools(table, {
                sRowSelect: "os",
                aButtons: [
                    { sExtends: "editor_create", editor: editor },
                    { sExtends: "editor_edit", editor: editor },
                    { sExtends: "editor_remove", editor: editor }
                ]
            });
            $(tableTools.fnContainer()).appendTo('#tblCentre_wrapper .col-sm-6:eq(0)');
        });
    

    A part from that, I am loading the Bootstrap integration libraries, as noted in this sample: https://editor.datatables.net/manual/styling/bootstrap

    Thanks!!

  • allanallan Posts: 63,692Questions: 1Answers: 10,500 Site admin
    Answer ✓

    Hi,

    Thanks very much for the code! I'm afraid I don't immediately see anything wrong with it there!

    Could you possibly try using Generator to build a package and check if that works with your table. I've just tried it myself with the following configuration:

    Generator

    That will create a package that is almost identical to your code above. The two differences are:

    1. The database connection
    2. No Bootstrap (I can't see how that would effect this at all though)

    To run the Generator package it should be a case of plugging in the options from my image above, then updating the database connection in the controller and pressing run.

    In my own test with that just now it doesn't have any issues with the addition of new rows.

    I'm really sorry you are having problems with something that should be completely straight forward here!

    Allan

  • fchopofchopo Posts: 4Questions: 1Answers: 0

    I'll try it again! Very odd as you said before!!

    Anyway, thank you very much!!!

  • allanallan Posts: 63,692Questions: 1Answers: 10,500 Site admin

    If you do get a chance, I'd be interested to hear how you get on with it!

    Allan

This discussion has been closed.