1

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?

1
  • I've encountered 500 error yesterday and the problem was my parameter is null Commented Jun 22, 2017 at 2:43

1 Answer 1

1

The DefaultModelBinder cannot set the value of fields. You need to make your collections properties by adding a getter/setter

public class RecordDetails
{
    public ScopeRecord ScopeRecord { get; set; }
    public List<FieldInfo> ProcedureFields { get; set; }
    public List<FieldInfo> RoomFields = { get; set; }
    ....
Sign up to request clarification or add additional context in comments.

1 Comment

Completely overlooked that. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.