You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CRUD应用Add/Edit功能异常求助:添加无数据、编辑报空引用异常

问题分析与修复方案

我看了你的代码,发现两个核心问题导致Add和Edit功能异常,咱们一步步来解决:

一、Add操作提示成功但无数据插入的原因与修复

问题根源

前端代码中,你错误地将$scope.Reclamations(用于存储所有工单的数组)当作单个工单对象来使用,传递给后端的是带有属性的数组,而不是符合Reclamation实体结构的对象。后端接收时无法正确绑定实体,导致Insert_Reclamation方法中的Rec参数为null,虽然代码返回了成功提示,但实际没有执行数据库插入操作。

另外,你初始化时给数组$scope.Reclamations添加了RecTitle等属性,这是错误的用法,数组不应该挂载单个实体的属性。

修复步骤(前端reclamations-controller.js)

  1. 删掉初始化代码中给数组添加属性的错误行:
// 删掉以下这些行
$scope.Reclamations.RecTitle = "";
$scope.Reclamations.RecDescription = "";
$scope.Reclamations.RecStatus = "";
$scope.Reclamations.RecResponsible = "";
$scope.Reclamations.RecComment = "";
  1. 修改InsertData方法的Add分支,创建单独的工单对象而非复用数组:
// Add分支修改后的代码
if (Action == "Submit") {
    // 创建新的工单对象
    var newReclamation = {
        RecTitle: $scope.RecTitle,
        RecDescription: $scope.RecDescription,
        RecStatus: $scope.RecStatus,
        RecResponsible: $scope.RecResponsible,
        RecComment: $scope.RecComment
    };
    $http({
        method: "post",
        url: "/Data/Insert_Reclamation",
        data: newReclamation // Angular会自动序列化,无需JSON.stringify
    }).then(function (response) {
        alert(response.data);
        $scope.GetAllData();
        // 清空表单
        $scope.RecTitle = "";
        $scope.RecDescription = "";
        $scope.RecStatus = "";
        $scope.RecResponsible = "";
        $scope.RecComment = "";
    })
}

二、Edit操作触发NullReferenceException的原因与修复

问题根源

  1. 前端Edit分支同样犯了和Add分支一样的错误:用数组传递数据,可能导致后端接收的Rec对象异常;
  2. 后端Update_Reclamation方法中,当根据RecId找不到对应工单时,RecObj会为null,直接访问RecObj的属性会触发空引用异常。

修复步骤

1. 前端修复(Edit分支)

修改InsertData的Edit分支,同样创建单独的更新对象:

} else {
    // 创建更新的工单对象
    var updateReclamation = {
        RecId: parseInt(document.getElementById("RecID_").value), // 确保转为数字类型
        RecTitle: $scope.RecTitle,
        RecDescription: $scope.RecDescription,
        RecStatus: $scope.RecStatus,
        RecResponsible: $scope.RecResponsible,
        RecComment: $scope.RecComment
    };
    $http({
        method: "post",
        url: "/Data/Update_Reclamation",
        data: updateReclamation
    }).then(function (response) {
        alert(response.data);
        $scope.GetAllData();
        // 清空表单并重置按钮状态
        $scope.RecTitle = "";
        $scope.RecDescription = "";
        $scope.RecStatus = "";
        $scope.RecResponsible = "";
        $scope.RecComment = "";
        document.getElementById("btnSave").setAttribute("value", "Submit");
        document.getElementById("btnSave").style.backgroundColor = "cornflowerblue";
        document.getElementById("spn").innerHTML = "Add New Reclamation";
    })
}

2. 后端修复(DataController.cs)

在Update_Reclamation方法中添加空值检查,避免空引用异常:

public string Update_Reclamation (Reclamation Rec) {
    if (Rec != null && Rec.RecId > 0) {
        using (GestReclamationDBEntities Obj = new GestReclamationDBEntities()) {
            Reclamation RecObj = Obj.Reclamations.Where(a => a.RecId == Rec.RecId).FirstOrDefault();
            // 新增空值判断
            if(RecObj != null) {
                RecObj.RecTitle = Rec.RecTitle;
                RecObj.RecDescription = Rec.RecDescription;
                RecObj.RecStatus = Rec.RecStatus;
                RecObj.RecResponsible = Rec.RecResponsible;
                RecObj.RecComment = Rec.RecComment;
                Obj.SaveChanges();
                return "Reclamation Updated Successfully";
            } else {
                return "Reclamation not found with the given ID!";
            }
        }
    } else {
        return "Invalid Reclamation data or ID!";
    }
}

额外优化建议

  1. 在后端Insert_Reclamation方法中,将异常信息返回给前端,这样插入失败时用户能收到错误提示:
try {
    Obj.Reclamations.Add(Rec);
    Obj.SaveChanges();
} catch (DbEntityValidationException ex) {
    ErrorPrinter(ex.EntityValidationErrors);
    return "Error inserting reclamation: " + ex.Message;
} catch (Exception ex) {
    Console.WriteLine("Exception not managed :");
    Console.WriteLine(ex.Message);
    return "Error inserting reclamation: " + ex.Message;
}
  1. 建议使用Angular的表单绑定替代原生DOM操作(比如document.getElementById),让代码更符合Angular的规范,也更易维护。

内容的提问来源于stack exchange,提问作者Hassan Jroundi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 22:27:37