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

ASP.NET Core编辑POST方法未与DB交互,修改不显示于卡片视图

问题求助:编辑功能POST请求无法保存数据

经过数月谷歌搜索和论坛发帖仍未解决问题,恳请各位提供思路。点击浏览器保存按钮后,编辑功能的HTTP POST方法无法保存用户输入。调试输出显示执行了Console.WriteLine("saving failed too"),说明修改未保存。进一步排查发现var ListingDTO_DBTable = _context.ListingDTO_DBTable.Find(Id);返回null,完全不清楚原因。

调试的代码如下:

[HttpGet]  
public JsonResult EditListingJ(int Id)    // 获取选中项详情
{ 
    try  
    {
        var ListingDTO_DBTable = _context.ListingDTO_DBTable.Find(Id);  
        if (ListingDTO_DBTable != null)  
        {  
            Console.WriteLine($"Got it");
            return Json(ListingDTO_DBTable);  
        } 
        
        return Json(null);  
    }  
    catch (Exception ex)  
    {  
        Console.WriteLine("so null nothing can be edited");
        return null;  
    }  
}  

[HttpPost]  
public JsonResult EditListingJ(int Id, ListingProjectsDTO _listingProjectsDto)      // 更新修改内容
{
    try  
    {
        var ListingDTO_DBTable = _context.ListingDTO_DBTable.Find(Id);         // 根据Id获取数据

        if (ListingDTO_DBTable != null)  
        {  
            _context.ListingDTO_DBTable.Update(ListingDTO_DBTable);              // 更新数据库
            _context.SaveChanges();  
            Console.WriteLine("saving finally success");
            return Json(ListingDTO_DBTable);  
        }  
    }  
    catch (Exception ex)  
    {  
        Console.WriteLine("saving failed");
        return null;  
    }

    Console.WriteLine("saving failed too");

    return Json(_listingProjectsDto);
}

排查方向和解决思路:

  • 检查POST请求的Id参数:用浏览器F12打开Network面板,找到对应的POST请求,查看请求参数里的Id值是否和GET请求时的一致,是否存在类型转换错误(比如前端传了字符串Id,后端int接收时出错)
  • 验证实体主键配置:确认ListingDTO_DBTable实体类的主键是否为Id,有没有通过[Key]特性或Fluent API正确配置。Find方法仅根据主键查询,主键配置错误会导致找不到数据
  • 替换Find方法排查跟踪问题:尝试用_context.ListingDTO_DBTable.FirstOrDefault(x => x.Id == Id)替代Find,如果能找到数据,说明DbContext对该实体的跟踪状态有问题
  • 核对数据库数据:直接查询数据库,确认Id对应的记录是否存在,有没有被误删除或修改
  • 修复POST方法的逻辑漏洞:当前代码没有把_listingProjectsDto的用户输入映射到数据库实体,就算找到记录也不会保存修改。需要先将DTO的字段赋值给实体,再执行Update和SaveChanges

修改后的POST方法示例:

[HttpPost]  
public JsonResult EditListingJ(int Id, ListingProjectsDTO _listingProjectsDto)      
{
    try  
    {
        var listingFromDb = _context.ListingDTO_DBTable.Find(Id);         
        if (listingFromDb != null)  
        {  
            // 根据实际字段映射DTO到实体
            listingFromDb.Title = _listingProjectsDto.Title;
            listingFromDb.Description = _listingProjectsDto.Description;
            // ...其他需要更新的属性

            _context.ListingDTO_DBTable.Update(listingFromDb);              
            _context.SaveChanges();  
            Console.WriteLine("saving finally success");
            return Json(listingFromDb);  
        }  
    }  
    catch (Exception ex)  
    {  
        Console.WriteLine($"saving failed: {ex.Message}"); // 打印异常详情便于排查
        return null;  
    }

    Console.WriteLine($"Record with Id {Id} not found");
    return Json(_listingProjectsDto);
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 18:05:15