首页 > 解决方案 > 使用 Jquery 在 Linq to sql 中删除不起作用?

问题描述

// 控制器代码

[HttpPost]
public JsonResult Delete(Int32 id)
{
                    // TODO: Add delete logic here
        TendorList t1 = db.TendorLists.Where(x => x.CompanyId == id).FirstOrDefault();
        db.TendorLists.DeleteOnSubmit(t1);
        db.SubmitChanges();``
        return Json(true, JsonRequestBehavior.AllowGet);
}

// jQuery 代码

$("body").on("click", "#tblStudent .Delete", function () {
        if (confirm("Do you want to delete this row?")) {`enter code here`
            var row = $(this).closest("tr");
            var CompanyId = row.find("span").html();
            $.ajax({
                type: "POST",
                url: "/Tendor/Delete",
                data: '{CompanyId: ' + CompanyId + '}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    row.remove();
                }
            });
        }
    });

标签: c#.netlinq-to-sql

解决方案


您将 id 作为 CompanyId 发送,但在后面的代码中尝试将其作为 Id 捕获。您应该将您的帖子数据更新为 data: '{id: ' + CompanyId + '}',


推荐阅读