从数据表中删除 jquery MVC5

delete from datatable jquery mvc5

本文关键字:jquery MVC5 删除 数据表      更新时间:2023-09-26

我正在尝试从数据表jquery中删除行以及sweetjs。

由于某种原因,我无法获得该行的 ID。 常数为 id=12(第一行)。

然后它转到我的控制器,进入 POST 删除方法,但总是 id=12(第一行),然后它在那里中断,因为它在数据库中找不到它。这是代码:

<script>
$(document).ready(function () {
    var t = $('.example').DataTable();
    $('.example tbody').on('click', 'tr', function () {
        if ($(this).hasClass('selected')) {
            $(this).removeClass('selected');
        }
        else {
            t.$('tr.selected').removeClass('selected');
            $(this).addClass('selected');
        }
        alert('Row index: ' + t.row(this).index()); //just cheking my id's
    });
    $('#button').click(function () {
        if (t.row('.selected')[0].length == 0) {
            swal("Morate selektovati red u tabeli.", "Ova operacija nije dozvoljena.", "warning");
            return false;
        }
        //var t = $('#report').DataTable();
        swal({
            title: "Da li ste sigurni?",
            text: "Posle brisanja se ne mogu povratiti podaci.",
            type: "warning",
            showCancelButton: true,
            confirmButtonClass: 'btn-warning',
            confirmButtonText: 'Da, obriši',
            cancelButtonText: "Odustani",
            closeOnConfirm: false,
            closeOnCancel: false
        },
            function (isConfirm) {
                if (isConfirm) {
                    $.ajax({
                        url: "/AdeccoViews/Delete",
                        contentType: 'application/json;',
                        data: JSON.stringify({ id: t.row('.selected').data()[0] }),
                        type: 'POST',
                        success: function (result) {
                            if (result.boolResponse == true) {
                                t.row('.selected').remove().draw(false);
                                swal("Obrisano!", "Veza između korisnika je obrisana!", "success");
                            }
                        },
                        error: function (valid) {
                            //window.location.href = "/Views/ERROR";
                            swal("Došlo je do greške!", "Molimo vas da pokušate ponovo!", "error");
                        }
                    });
                } else {
                    swal("Brisanje poništeno", "SalesActivity je i dalje tuuuuu", "info");
                }
            });
    });
});

<button type="button" id="button">Delete</button>

控制器:

[HttpPost, ActionName("Delete")]
    //[ValidateAntiForgeryToken]
    //[HttpPost]
    public ActionResult DeleteConfirmed(int id)
    {
        object obj;
        try
        {
            AdeccoView adeccoView = db.AdeccoViews.Find(id);
            db.AdeccoViews.Remove(adeccoView);
            db.SaveChanges();
            obj = new
            {
                boolResponse = true
            };
            return Json(obj);
            //return RedirectToAction("Index");
        }
        catch (Exception ex)
        {
            throw;
        }
    }

这是问题所在:

data: JSON.stringify({ id: t.row('.selected').data()[0] }),

您的 ajax 是硬编码的,以获取第一行 ( [0] )。

或者,此行实际上不起作用:

t.$('tr.selected').removeClass('selected');

这导致第一行(最初选择的)仍然具有该类。

您可以使用Chrome调试工具(F12)进行调试,以在此行上中断并查看分配给id的内容。