gpt4 book ai didi

javascript - JSON.stringify 子数组在 Controller 上为空

转载 作者:太空宇宙 更新时间:2023-11-04 15:32:20 25 4
gpt4 key购买 nike

有很多此类问题,查看所有这些问题,我不太确定我的设置有何不同。

我尝试了 ajax 数据的几种不同变体以及如何格式化 JSON,但这似乎让我最接近。我之前已经这样做过好几次了,但从未遇到过这个问题。

当我点击 Controller 时,modelDetails 是一个完整的对象,并且 ScopeRecord 填充了数据。但是,每个子数组都是空的——包括 ProcedureFields,这是没有意义的。它不是 null,但计数为 0。

简化的js:

$("#submitButton")
.click(function() {
var result = {};
result.ScopeRecord = {};
result.ScopeRecord.ReferenceNumber = "testing123";
result.RoomFields = [];
result.BioFields = [];
result.ReprocessingFields = [];
result.CultureFields = [];
result.ProcedureFields = [];

var fieldInfo = {};

//examDate
fieldInfo.FieldID = 1;
fieldInfo.FieldValue = "test me";
fieldInfo.ItemHistoryID = 3;
fieldInfo.AssociationID = 2;
fieldInfo.IsModified = 1;
result.ProcedureFields.push(fieldInfo);
result.ProcedureFields.push(fieldInfo);
result.ProcedureFields.push(fieldInfo);

var options = {};
options.url = "/MyController/SaveDetails";
options.type = "POST";
options.traditional = true;
var test = JSON.stringify(result);
options.data = test;
options.contentType = "application/json; charset=UTF-8";
options.dataType = "json";
$.ajax(options);
});

我的请求数据:

"{
"ScopeRecord":
{
"ReferenceNumber":"testing123"
},
"RoomFields":[],
"BioFields":[],
"ReprocessingFields":[],
"CultureFields":[],
"ProcedureFields":
[{
"FieldID":1,
"FieldValue":"test me",
"ItemHistoryID":3,
"AssociationID":2,
"IsModified":1
},
{
"FieldID":1,
"FieldValue":"test me",
"ItemHistoryID":3,
"AssociationID":2,
"IsModified":1
},
{
"FieldID":1,
"FieldValue":"test me",
"ItemHistoryID":3,
"AssociationID":2,
"IsModified":1
}]
}"

Controller :

[HttpPost]
public ActionResult SaveDetails(RecordDetails modelDetails)
{
....
}

型号:

public class RecordDetails
{
public ScopeRecord ScopeRecord { get; set; }
public List<FieldInfo> ProcedureFields = new List<FieldInfo>();
public List<FieldInfo> RoomFields = new List<FieldInfo>();
public List<FieldInfo> BioFields = new List<FieldInfo>();
public List<FieldInfo> ReprocessingFields = new List<FieldInfo>();
public List<FieldInfo> CultureFields = new List<FieldInfo>();
}

public class FieldInfo
{
public int ItemHistoryID { get; set; }
public int FieldID { get; set; }
public string FieldValue { get; set; }
public bool IsModified { get; set; }
public int? AssociationID { get; set; }
}

我尝试过 options.data = { modelDetails : JSON.stringify(result) }; 但这给了我一个 500 错误。

我缺少什么部分?

最佳答案

DefaultModelBinder 无法设置字段的值。您需要通过添加 getter/setter 来创建集合属性

public class RecordDetails
{
public ScopeRecord ScopeRecord { get; set; }
public List<FieldInfo> ProcedureFields { get; set; }
public List<FieldInfo> RoomFields = { get; set; }
....

关于javascript - JSON.stringify 子数组在 Controller 上为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44689194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com