2017-09-03 40 views
0

我Ajax調用,我將通過價值後端刪除行的數據庫通過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);在我有錯誤

嚴重性代碼說明項目文件李NE抑制狀態 錯誤CS1503參數1:不能從轉換'到 'RS_Main.Models.Appointment' RS_Main C:\用戶\ nemes \來源\回購\ RIS_Project_New \ RS_Main \ \控制器28 CalendarController.cs主動

如何從數據庫中刪除行?

+3

只是刪除'。選擇(X =>新{標題= x.Title})'位 - 'VAR項目= db.Appointments.Where(X => x.Title ==標題).FirstOrDefault ();' –

回答

1

隨着db.remove功能,你需要表示表確切的對象,所以你不需要選擇

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(); 
1
db.Appointments.FirstOrDefault(x => x.Title == title).Remove(); 
db.SaveChanges();