There are a lot of these types of questions, and looking at all of them, I'm not particularly sure what the difference is with my setup.
I've tried a few different variations of the ajax data and how to format the JSON, but this seems to get me the closest. I've done this quite a few times before, and I never ran into this issue.
When I hit the controller, modelDetails is a full object, and ScopeRecord is populated with data. However, each child array is empty -- including ProcedureFields, which isn't making sense. It's not null, but the count is 0.
Simplified 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);
});
My data on the request:
"{
"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)
{
....
}
Model:
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; }
}
I've tried options.data = { modelDetails : JSON.stringify(result) }; but that gives me a 500 error.
What part am I missing?