控制器

时间:2017-06-22 02:33:43

标签: javascript jquery json ajax asp.net-mvc

有很多这类问题,看着它们,我不太清楚我的设置有什么不同。

我已经尝试了ajax数据的一些不同变体以及如何格式化JSON,但这似乎让我最接近。我以前做了很多次,我从来没有遇到过这个问题。

当我点击控制器时,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
         }]
  }"

控制器:

[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错误。

我错过了哪一部分?

1 个答案:

答案 0 :(得分:1)

DefaultModelBinder无法设置字段的值。您需要通过添加getter / setter

来创建集合属性
public class RecordDetails
{
    public ScopeRecord ScopeRecord { get; set; }
    public List<FieldInfo> ProcedureFields { get; set; }
    public List<FieldInfo> RoomFields = { get; set; }
    ....