JSON参数反序列化不一致

时间:2016-03-17 15:35:31

标签: .net json asp.net-mvc-5

一方面我有这个模型:

public class DocumentAccessPermissions
{
    public string Name { get; set; }

    public bool IsGroup { get; set; }

    public bool ViewDetails { get; set; }

    public bool Download { get; set; }

    public bool EditDetails { get; set; }

    public bool Modify { get; set; }

    public bool Delete { get; set; }
}

我将它发送到控制器,如下所示:

var documentId = $('#hdnDocumentId', popup).val();
var items = [];
$.each(someData, function (index, item) {
    items.push({
        Name: item.Name,
        IsGroup: item.IsGroup,
        ViewDetails: item.ViewDetails,
        EditDetails: item.EditDetails,
        Modify: item.Modify,
        Download: item.Download,
        Delete: item.Delete
    });
});

$.ajax({
    url: $('#hdnSaveLink', popup).val(),
    type: 'post',
    data: {
        id: documentId,
        permissions: items
    },
    dataType: 'json',
    success: function (response) {
    }
});

控制器接收它:

public async Task<JsonResult> AccessManager_Save(string id, Models.DocumentAccessPermissions[] permissions)
{
    var documents = new Models.Documents();
    bool success = await documents.SetAccessPermissions(id, permissions);
    return Json(success);
}

它有效。我收到了正确反序列化的对象数组,没有任何额外的工作。

另一方面,我有这个模型:

public class BillingsCategoryTermItemForEdit
{
    public int ID { get; internal set; }
    public bool? InvoiceAttachement1 { get; internal set; }
    public short? InvoicingStartToEndDate { get; internal set; }
    public short? PreInvoicingDays { get; internal set; }
    public string PreInvoicingDaysType { get; internal set; }
    public short? Reccurence { get; internal set; }
    public short? ReccurenceDay { get; internal set; }
    public short? ReccurenceDayOfWeek { get; internal set; }
    public short? ReccurenceType { get; internal set; }
    public short? ReccurenceWeek { get; internal set; }
}

我正在尝试发送这样的数据:

$.ajax({
    url: '/GroupM/InvoiceTerms/EditInvoicingFrequency_Save',
    type: 'post',
    data: {
        item: {
            ID: ID,
            ReccurenceType: ReccurenceType,
            Reccurence: Reccurence,
            ReccurenceDay: ReccurenceDay,
            ReccurenceDayOfWeek: ReccurenceDayOfWeek,
            ReccurenceWeek: ReccurenceWeek,
            InvoiceAttachement1: InvoiceAttachement1,
            InvoicingStartToEndDate: InvoicingStartToEndDate,
            PreInvoicingDaysType: PreInvoicingDaysType,
            PreInvoicingDays: PreInvoicingDays
        },  
        mediumGroupIDs: resultingMediumTypes
    },
    dataType: 'json',
    success: function (message) {
    }
});

我试图像这样接收它,假设它将像前面的例子一样工作:

public virtual async Task<JsonResult> EditInvoicingFrequency_Save(Models.BillingsCategoryTermItemForEdit item, string mediumGroupIDs)
{
    string username = UserManager.GetFullUsername();
    var billings = new Models.Billings();
    bool success = await billings.Save(item, mediumGroupIDs, username, MVC.GroupM.Name);
    return Json(success);
}

它不起作用。我正在使用零和空值(默认值)获取对象。我检查了发送到服务器的请求,看起来没问题。发送数据,仅在第二个示例中,它未正确反序列化。

如果有任何见解,我将不胜感激。

顺便说一下。我知道我可以使用JSON.stringify然后反序列化服务器端的字符串。我不是在寻找那个解决方案。我正在寻找这种方式在第一个但不在第二个例子中起作用的原因。

1 个答案:

答案 0 :(得分:1)

可能是因为第二类内部有setter吗?我的意思是可序列化属性应该同时包含public set和get访问器,否则序列化程序不应该有权访问设置值。