CRUD添加新行而不是更新

时间:2019-06-16 00:38:31

标签: jquery asp.net ajax rest asp.net-ajax

.Net应用程序中的数据更新出现问题。当我尝试更新时,使用新ID创建新行,而不是更新现有ID。

我看不出问题出在哪里。也许有人可以帮助解决问题。

这是控制器中的方法:

     public JsonResult SaveDataInDatabase (StudentViewModel model)
    {
        var result = false;
        try
        {
            if (model.StudentId > 0)
            {
                tblStudent Stu = db.tblStudent.SingleOrDefault(x => x.IsDeleted == false && x.StudentId == model.StudentId);
                Stu.StudentName = model.StudentName;
                Stu.Email = model.Email;
                Stu.DepartmentId = model.DepartmentId;
                db.SaveChanges();
                result = true;
            }
            else
            {
                tblStudent Stu = new tblStudent();
                Stu.StudentName = model.StudentName;
                Stu.Email = model.Email;
                Stu.DepartmentId = model.DepartmentId;
                Stu.IsDeleted = false;
                db.tblStudent.Add(Stu);
                db.SaveChanges();
                result = true;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return Json(result, JsonRequestBehavior.AllowGet);
    }

和jQuery函数:

      $("#SaveStudentRecord").click(function () {
    var data = $("#SubmitForm").serialize();
    $.ajax({
        type: "Post",
        url: "/Home/SaveDataInDatabase",
        data: data,
        success: function (result) {
            alert("Success!..");
            window.location.href = "/Home/index";
            $("#MyModal").modal("hide");
        }
    })
})

这是模型视图:

      public class StudentViewModel
{
    public int StudentId { get; set; }

    public string StudentName { get; set; }

    public string Email { get; set; }

    public bool? IsDeleted { get; set; }

    public int? DepartmentId { get; set; }

    public String DepartmentName { get; set; }
}

GetStudentByID方法:

      public JsonResult GetStudentById (int StudentId)
    {
        tblStudent model = db.tblStudent.Where(x => x.StudentId == StudentId).SingleOrDefault();
        string value = string.Empty;
        value = JsonConvert.SerializeObject(model, Formatting.Indented, new JsonSerializerSettings
        {
            ReferenceLoopHandling = ReferenceLoopHandling.Ignore
        });
        return Json(value, JsonRequestBehavior.AllowGet);
    }

1 个答案:

答案 0 :(得分:0)

正如您在评论中提到的那样,将代码更改为此即可

tblStudent Stu = db.tblStudent.FirstOrDefault(x => x.IsDeleted == false && x.Email == model.Email);
if (Stu != null)
{
     Stu.StudentName = model.StudentName;
     Stu.Email = model.Email;
     Stu.DepartmentId = model.DepartmentId;
     db.Entry(Stu).State = EntityState.Modified;
     db.SaveChanges();
     result = true;
 }
 else
 {
     tblStudent Stu = new tblStudent();
     Stu.StudentName = model.StudentName;
     Stu.Email = model.Email;
     Stu.DepartmentId = model.DepartmentId;
     Stu.IsDeleted = false;
     db.tblStudent.Add(Stu);
     db.SaveChanges();
     result = true;
  }

只需确保StudentId列为自动递增