mark_spencer
mark_spencer

Reputation: 393

Delete row in database by ajax call

I have ajax call where I will pass value to back-end

I need to find data in database by this value and then delete it

I write this code on back-end

 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);

    }

But in this row db.Appointments.Remove(item); I have error

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

How I can delete row from database?

Upvotes: 0

Views: 192

Answers (2)

user8507737
user8507737

Reputation:

With db.remove function you need exact object representing the table, so you dont need 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

}

And while using string change it into lowercase or to upper case for better performance.

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

Upvotes: 1

Afnan Makhdoom
Afnan Makhdoom

Reputation: 654

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

Upvotes: 1

Related Questions