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

ASP.NET Razor Pages接收DataTables选中数组字段全空问题

解决ASP.NET Razor Pages中DataTables选中数组传递后字段为Null的问题

核心问题原因

你的RemoveAirports实体类使用的是公共字段(Field),而ASP.NET Core的模型绑定系统仅支持绑定带有get/set访问器的属性(Property),这就导致即使请求中数组的元素数量正确,每个元素的字段值也无法被绑定,全部为null。

解决方案

将实体类中的字段修改为属性,以下是两种可选方式:

方式1:保持与前端JSON键名一致的属性

直接将字段改为带get/set的属性,键名和前端传递的小驼峰命名保持一致:

public class RemoveAirports
{
    public string continentName { get; set; }
    public string countryName { get; set; }
    public string regionName { get; set; }
    public string regionCode { get; set; }
    public string airport { get; set; }
    public string airportName { get; set; }
}

方式2:使用标准帕斯卡命名+显式JSON映射

如果习惯使用C#的帕斯卡命名规范,可以通过JsonPropertyName特性显式指定JSON键名,确保和前端传递的字段匹配:

using System.Text.Json.Serialization;

public class RemoveAirports
{
    [JsonPropertyName("continentName")]
    public string ContinentName { get; set; }
    
    [JsonPropertyName("countryName")]
    public string CountryName { get; set; }
    
    [JsonPropertyName("regionName")]
    public string RegionName { get; set; }
    
    [JsonPropertyName("regionCode")]
    public string RegionCode { get; set; }
    
    [JsonPropertyName("airport")]
    public string Airport { get; set; }
    
    [JsonPropertyName("airportName")]
    public string AirportName { get; set; }
}

其他确认点

你的前端AJAX代码已经做了正确配置,无需修改:

  • 已设置contentType: "application/json; charset=utf-8",确保请求体以JSON格式发送
  • 已携带RequestVerificationToken,符合Razor Pages的防跨站请求伪造要求
  • 已将选中数据序列化为JSON字符串后发送

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 04:06:29