DataTables .NET Editor remove action not deleting a record.

DataTables .NET Editor remove action not deleting a record.

tomslloydtomslloyd Posts: 12Questions: 3Answers: 0
edited July 10 in Free community support

I need some help to point out what i am doing wrong or what i am am not doing right,

I have the following code that all works bar the delete action

Controller Method in .NET

        [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
        [ValidateAntiForgeryToken()]
        public ActionResult GetFlagsList()
        {
            var formData = HttpContext.Request.Form;

            using (var db = new Database("mysql", _dbFactory.CreateConnection(ConnectionDatabase.CustomerSpecific)))
            {
                var editor = new Editor(db, "pmt_flags f", "id")
                    .Field(new Field("f.id").Set(true))
                    .Field(new Field("f.source").Set(true)
                        .Options(new Options()
                            .Table("table1 invd3")
                            .Value("id").Label("name").Where(r => {
                                r.Where("inv_type_id", "(108, 111)", "IN", false);
                            })
                            .Order("id")
                        )
                    )
                    .Field(new Field("invd1.name").Set(false))
                    .Field(new Field("f.destination").Set(true)
                        .Options(new Options()
                            .Table("table2 invd3")
                            .Value("id").Label("name").Where(r => {
                                r.Where("inv_type_id", "(108, 111)", "IN", false);
                            })
                            .Order("id")
                        )
                    )
                    .Field(new Field("invd2.name").Set(false))
                    .Field(new Field("f.flag")
                        .Set(true)
                        .Options(new Options()
                            .Table("managed_values mv")
                            .Value("m_value").Label("mv.d_value").Where(r => {
                                r.Where("mv.mvg_id", "(SELECT id FROM managed_values_groups mvg WHERE mvg.code = 'MIGRATION_FLAGS')", "IN", false);
                            })
                            .Order("id")
                        )
                    )
                    .Field(new Field("f.type").Set(true))
                    .Field(new Field("f.enabled").Set(true))
                    .Field(new Field("f.description").Set(true))
                    .Field(new Field("f.create_by").Set(true))
                    .Field(new Field("f.update_by").Set(true))
                    .LeftJoin("table1 invd1", "f.source", "=", "invd1.id")
                    .LeftJoin("table1 invd2", "f.destination", "=", "invd2.id");

                editor.PreCreate += (sender, e) =>
                {
                    //if (readOnly)
                    //{
                    //    e.Cancel = true;
                    //    return;
                    //}
                    Dictionary<string, object> flagData = ((Dictionary<string, object>)e.Values["f"]);
                    User user = GetUser();

                    editor.Field("f.create_by").SetValue(user.Id);
                    editor.Field("f.update_by").SetValue(user.Id);


                    if (!isValidFlag(flagData))
                    {
                        // We already have an entry for this service Type
                        throw new Exception("Flag Could not be created, please check the values and try again.");
                    }
                };

                editor.PreEdit += (sender, e) =>
                {
                    Dictionary<string, object> flagData = ((Dictionary<string, object>)e.Values["f"]);
                    User user = GetUser();

                    editor.Field("f.update_by").SetValue(user.Id);


                    if (!isValidFlag(flagData, "edit"))
                    {
                        // We already have an entry for this service Type
                        throw new Exception("Flag Could not be updated, please check the values and try again.");
                    }
                };

                editor.PreRemove += (sender, e) =>
                {
                    Dictionary<string, object> flagData = ((Dictionary<string, object>)e.Values["f"]);
                    User user = GetUser();

                    if (!isValidFlag(flagData, "edit"))
                    {
                        // We already have an entry for this service Type
                        throw new Exception("Flag Could not be created please check the values and try again.");
                    }
                };

                var response = editor.Transaction(true).Debug(true).Process(formData).Data();

                return Json(response, JsonRequestBehavior.AllowGet);

            }
        }

Java Script

        var isAdmin = @Html.Raw(Json.Encode(Model.isAdmin));

        var editor = new $.fn.dataTable.Editor({
            "ajax": {
                "url": "@Url.Action("GetFlagsList", "CustomerMigration", new { boid = 1 })",
                "type": "POST",
                "data": function (d) {
                    d.__RequestVerificationToken = $('input[name=__RequestVerificationToken]').val();
                }
            },
            table: "#tblSC",
            fields: [
                {
                    name: "f.id",
                    type: "hidden"
                },
                {
                    name: "f.source",
                    type: "select",
                    placeholder: "--- select a source ---"
                },
                {
                    name: "f.destination",
                    type: "select",
                    placeholder: "--- select a destination ---"
                },
                {
                    name: "f.flag",
                    type: "select",
                    placeholder: "--- select a flag ---"
                },
                {
                    name: "f.type",
                    type: "select",
                    placeholder: "--- select a type ---",
                    options: [
                        { label: "VOICE", value: "VOICE" },
                        { label: "PON", value: "PON" },
                        { label: "DATA", value: "DATA" },
                        { label: "L3", value: "L3" }
                    ]
                },
                {
                    name: "f.description",
                    type: "textarea",
                },
                {
                    name: "f.enabled",
                    type: "select",
                    placeholder: "--- select a enabled ---",
                    options: [
                        { label: "Yes", value: 1 },
                        { label: "No", value: 0 }
                    ]
                },

            ]
        });

        var table = $('#tblSC').DataTable({
        ajax:
            {
                "url": "@Url.Action("GetFlagsList", "CustomerMigration", new { boid = 1 })",
                "type": "POST",
                "data": function (d) {
                    d.__RequestVerificationToken = $('input[name=__RequestVerificationToken]').val();
                },
            },
            serverSide: true,
            searching: true,
            orderable: true,
            processing: true,
            dom: '<"row"<"col-sm-6"B>><"row-fluid"<"col-sm-6"l><"col-sm-6"f>>rt<"row-fluid"<"col-sm-6"i><"col-sm-6"p>>',
            select: true,
            columns: [
                { "data": "f.id", "searchable": true},
                { "data": "invd1.name", "searchable": true},
                { "data": "invd2.name", "searchable": true },
                { "data": "f.flag", "searchable": true, render: function (data) {
                        switch (data) {
                            case "0": default: return "<i class='fa fa-circle flag-green'></i>";
                            case "1": return "<i class='fa fa-circle flag-amber'></i>";
                            case "2": return "<i class='fa fa-circle flag-red'></i>";
                        }
                    }
                },
                { "data": "f.type", "searchable": true },
                { "data": "f.enabled", "searchable": true, render: function (data) {
                        switch (data) {
                            case "0": default: return "<i class='fa fa-times text-danger'></i>";
                            case "1": return "<i class='fa fa-check text-success'></i>";

                        }
                    }
                }
            ],
            buttons: [
                { extend: "create", editor: editor },
                { extend: "edit", editor: editor },
                { extend: "remove", editor: editor },
            ],
            initComplete: function () {
                $(".dt-buttons").prepend('<button class="btn btn-secondary show-text-hover" data-href="@Url.Action("Index", "CustomerMigration", new { boid = Model.CompanyId })" type="button"> <i class="fa fa-arrow-left"></i> <span>Back</span> </button>')
                if (!isAdmin) {
                    $('.buttons-create').addClass('disabled'); // Disable the button visually if isAdmin is false
                }
            }
        });

    });

the table loads correctly i can edit and create with no issues, but not the remove action it sends the data in the request with the action remove with the row data, and then i get the second reload the table data, but the record to be deleted is still there.

can someone please point me in the right direction

Editor Version: DataTables-Editor-Server.2.0.5 // net48\DataTables-Editor-Server.dll

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin

    Nothing immediately stands out to me as causing an issue there. Can you show me the data sent to the server and also the JSON response in return please?

    Thanks,
    Allan

  • tomslloydtomslloyd Posts: 12Questions: 3Answers: 0

    This is the post request sent for the delete

    ##Query String 
    boid: 1
    
    ##Form Data
    data[row_42][DT_RowId]: row_42
    data[row_42][f][id]: 42
    data[row_42][f][source]: 745
    data[row_42][f][destination]: 734
    data[row_42][f][flag]: 1
    data[row_42][f][type]: L3
    data[row_42][f][enabled]: 0
    data[row_42][f][description]: 
    data[row_42][f][create_by]: 10478
    data[row_42][f][update_by]: 10478
    data[row_42][invd1][name]: Adtran SDX 6330-48
    data[row_42][invd2][name]: Nokia XS-010G-Q
    action: remove
    __SCRFTOKEN: the value
    

    Json Response form that

    {
        "draw": null,
        "data": [],
        "recordsTotal": null,
        "recordsFiltered": null,
        "error": null,
        "fieldErrors": [],
        "id": null,
        "meta": {},
        "options": {},
        "searchPanes": {
            "options": {}
        },
        "files": {},
        "upload": {
            "id": null
        },
        "debug": [],
        "cancelled": []
    }
    

    then there is another request that returns all the table data after this one and that one look normal it returns the rows but the deleted one is not deleted.

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Answer ✓

    "debug": [],

    Suggests that no query was executed against the database. Which appears to agree with that you are seeing when you reload the page.

    I know it is a bit of a pain, but could you try removing the alias for the table name and see if that helps? i.e.

    new Editor(db, "pmt_flags f", "id")
    

    would be:

    new Editor(db, "pmt_flags", "id")
    

    And wherever you have f. in the fields (C# and Javascript) it would need to be updated to be pmt_flags. I'm not certain that is the issue, but it would be the starting point.

    Allan

  • tomslloydtomslloyd Posts: 12Questions: 3Answers: 0

    It creates records with the alias in place, but sure will give that a go maybe the delete record is different for some reason.

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin

    Yeah, it follows a slightly different code path and I had some issues with aliases in the past. It is possible there is still an issue there. I'll try it later on as well.

    Allan

  • tomslloydtomslloyd Posts: 12Questions: 3Answers: 0

    Hey removing the alias worked, can you please make a fix to allow the DELETE to work with aliases as this can only help

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin

    Thank you for confirming that and good to hear there is a workaround for the moment.

    I've logged this so I can look at it for the next release of Editor.

    Allan

Sign In or Register to comment.