Link to home
Start Free TrialLog in
Avatar of bmanmike39
bmanmike39

asked on

How do I write the code to delete a table row in my db from my MVC controller? (ASP.NET, MVC)

I'm using the Entity framework in my MVC project. I would like to delete a record when the user navigates to a specific controller. What is wrong with my code below? And how can I accomplish this?

The table name is: fooItem

MYENTITIES oe = new MYENTITIES();
fooItem j = oe.fooItems.ToList().Where(d => d.fooItemId == Id).First();

oe.fooItems.Remove(j);
oe.SaveChanges();

Open in new window

SOLUTION
Avatar of Lokesh B R
Lokesh B R
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SQL Server topic removed.
Avatar of bmanmike39
bmanmike39

ASKER

I'm trying to delete the row from my records, not check if it  exist.
Yes, debug the code see whether it works or not.
I'm  a little confused. Doesn't this code do the same thing.

NOTE: It's very Important that It only deletes the sing record.

MYENTITIES oe = new MYENTITIES();
            var f = oe.fooItems.Where(s => s.fooItemId = Id).FirstOrDefault();
            oe.fooItems.Remove(f);
            oe.SaveChanges();

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you!  This was just what I was looking for.