JSON serializing of complex object MVC3

淺唱寂寞╮ 提交于 2019-12-22 07:19:41

问题


I have a problem, I don't seem to know how to serialize a object of type:

public class SchedulingCalendarMonth
{
    public List<SchedulingCalendarWeek> Weeks {get; set; }
}
public class SchedulingCalendarWeek
{
    public List<SchedulingCalendarDay> Days { get; set; }
}

public class SchedulingCalendarDay
{
    public int Id { get; set; }
    public bool SomeBoolProperty { get; set; }
}

I've tried something like this:

<script type="text/javascript">

$(document).ready(function () {

    var Month =
    {
        Weeks: []
    };
    var Weeks = { Days: [{ Id: 1, SomeBoolProperty: true }, { Id: 4, SomeBoolProperty: true }, { Id: 43, SomeBoolProperty: false}] };

    Month.Weeks = Weeks;

    $.ajax({
        url: '/Home/Test',
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify({ month: Month }),
        contentType: 'application/json; charset=utf-8',
        success: function (result) {
            alert(result.Result);
        }
    });
});

But all i get on the controller action is Month object with Weeks = null.

Any ideas?


回答1:


The reason is your Month.Weeks is an array so in order to add one to it, you can't just do it like the above Month.Weeks = Weeks; but rather Month.Weeks.push(Weeks); Give it a go.

Thanks.



来源:https://stackoverflow.com/questions/6512467/json-serializing-of-complex-object-mvc3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!