通过 ajax 调用删除数据库中的行

Delete row in database by ajax call

我有 ajax 调用,我将在其中将值传递给后端

我需要通过这个值在数据库中查找数据,然后删除它

我在后台写这段代码

 public ActionResult DeletingInternal(string title)
    {

        var item = db.Appointments.Where(x => x.Title == title)
            .Select(x => new
            {
                title = x.Title
            }).FirstOrDefault();
        db.Appointments.Remove(item);

    }

但是这一行db.Appointments.Remove(item);我有错误

Severity Code Description Project File Line Suppression State Error CS1503 Argument 1: cannot convert from '' to 'RS_Main.Models.Appointment' RS_Main C:\Users\nemes\Source\Repos\RIS_Project_New\RS_Main\Controllers\CalendarController.cs 28 Active

如何从数据库中删除行?

db.Appointments.FirstOrDefault(x => x.Title == title).Remove();
db.SaveChanges();

使用 db.remove 函数你需要代表 table 的确切对象,所以你不需要 select

 public ActionResult DeletingInternal(string title)
    {

        var item = db.Appointments.Where(x => x.Title == title).FirstOrDefault();

    db.Appointments.Remove(item);

    db.SaveChanges(); // this saves the changes

}

并且在使用字符串时将其更改为小写或大写以获得更好的性能。

db.Appointments.Where(x => x.Title.ToLower() == title.ToLower() ).FirstOrDefault();