从datatable jquery mvc5中删除

时间:2016-03-04 23:03:55

标签: javascript jquery asp.net-mvc datatables

我试图将数据表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;
        }
    }

1 个答案:

答案 0 :(得分:0)

这是问题所在:

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

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

或者,这一行实际上并不起作用:

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

导致第一行(最初被选中)仍然具有该类。

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