本文介绍了复杂对象MVC3的JSON序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个问题,我似乎不知道如何序列类型的对象:
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);
}
});
});
但我得到的控制器动作与周= NULL月份对象。
But all i get on the controller action is Month object with Weeks = null.
任何想法?
推荐答案
原因是你的 Month.Weeks
是一个数组,所以为了增加一到,你不能仅仅做到像上面的 Month.Weeks =周;
而是 Month.Weeks.push(周);
搏一搏。
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.
感谢。
这篇关于复杂对象MVC3的JSON序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!