Friday, July 3, 2015

Entity FrameWork With MVC5 Part 4 (Delete)

we earlier saw how easy and efficient it was to add a record and read a record .
same way we can do delete operation

Note : we first find the id of the record to be deleted and then hit delete . 

so below are the action result to delete the records (get and post methods)



[HttpGet]
        public ActionResult Delete(int id)
        {
            ProjectWBSContext pwbs = new ProjectWBSContext();
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Projects projects = pwbs.Projects.Find(id);
            if (projects == null)
            {
                return HttpNotFound();
            }
            return View(projects);
        }
        ///Project/Delete/2
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
          

                ProjectWBSContext pwbs = new ProjectWBSContext();
                Projects projects = pwbs.Projects.Find(id);

                pwbs.Projects.Remove(projects);
                pwbs.SaveChanges();

                return RedirectToAction("Index");
         

           


        }

Create a view and then hit run . you will see the UI to delete the records 



No comments:

Post a Comment