简体   繁体   English

通过使用ajax从数据库EntityFramework中删除记录

[英]Delete record by using ajax from database EntityFramework

I am beginner and trying to delete the record from database by using JavaScript , Ajax and Json in MVC Entity Framework . 我是初学者,正在尝试通过MVC Entity Framework中的 JavaScriptAjaxJson从数据库中删除记录。 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 我的JavaScript代码是

<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. 如果有人帮助我,我将非常感激。

You need to add your model id in jquery data tag: 您需要在jquery数据标签中添加模型ID:

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

Then in javascript code: 然后在javascript代码中:

$("#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")
        });

I think that the error comes from the type attribute of your ajax call. 我认为该错误来自ajax调用的type属性。 You assign "delete" to this attribute, but it must be "POST": 您为此属性分配“删除”,但它必须为“ POST”:

 $.ajax({
          url: "@Url.Action("Delete","AjaxStudent")",
          type: "POST", // here is your problem,
          data: { StudentId: stdId },
          dataType: 'json',
          success: function() {                 
                alert("Deleted");
            },
          error: function(dat) {
                alert(data.x);
            }
        });

And the action method in your controller must be decorated with [httppost] : 并且控制器中的操作方法必须用[httppost]装饰:

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

After lots of time I am able to resolve my problem. 经过很多时间,我能够解决我的问题。

Now my javaScript code 现在我的javaScript代码

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

you should try by changing : 您应该尝试通过更改:

 $.ajax({
                url: "/AjaxStudent/Delete",
                type: 'Delete',
                data: {
                    StudentId: stdId
                }

to: 至:

 $.ajax({
            url: "/AjaxStudent/Delete",
            type: 'Delete',
            data: {
                'id':stdId
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM