Delete record by using ajax from database EntityFramework

asp.net-ajax asp.net-mvc-3 entity-framework-6 javascript jquery

Question

I am beginner and trying to delete the record from database by using JavaScript, Ajax and Json in MVC Entity Framework. But my delete button is not working well.

In controller class my action code is

public ActionResult Delete(int id) {
            using (StudentContext db = new StudentContext()) {
                Student std = db.Student.Where(x => x.Id == id).FirstOrDefault<Student>();
                db.Student.Remove(std);
                db.SaveChanges();
              }
              return Json(true, JsonRequestBehavior.AllowGet);
        }

and my JavaScript code is

<button id='deleteRecord'>delete</button>

$("#deleteRecord").click(function () {
                var StudentId = $(this).val();
                var stdId = parseInt(StudentId);
                $.ajax({
                    url: "/AjaxStudent/Delete",
                    type: 'Delete',
                    data: {
                        StudentId: stdId
                    }
                }).done(function () {
                    alert("Deleted")
                });
            });

        }).error(function () {
            alert("Failed")
        });

I shall be very thankful if anybody help me.

1
1
6/20/2018 6:38:01 PM

Accepted Answer

You need to add your model id in jquery data tag:

<button id='deleteRecord' data-model-id="@model.Id">delete</button>

Then in javascript code:

$("#deleteRecord").click(function () {
                var StudentId = $(this).data("model-id");
                var url = "/AjaxStudent/Delete/" + StudentId;
                $.ajax({
                    url: url,
                    type: 'Delete',
                }).done(function () {
                    alert("Deleted")
                });
            });
        }).error(function () {
            alert("Failed")
        });
2
6/20/2018 11:21:29 PM

Popular Answer

After lots of time I am able to resolve my problem.

Now my javaScript code

<button class='deleteRecord' data-stid=" + students[i].Id + " >delete</button> 
$(".deleteRecord").click(function () {
                var StudentId1 = $(this).data("stid");
                debugger;
                $.ajax({
                    url: "/AjaxStudent/Delete/" + StudentId1,
                    type: "Post"
                }).done(function () {

                    getAllStudents();

                    });
                });
            });

Controller.cs

public ActionResult Delete(int id) {
            using (StudentContext db = new StudentContext()) {
                Student std = db.Student.Where(x => x.Id == id).FirstOrDefault();
                db.Student.Remove(std);
                db.SaveChanges();
            }

            return Json(true, JsonRequestBehavior.AllowGet);
        }


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow