我想传递一个复杂的对象数组,并且evrey对象还有另外两个对象数组。WebForms - 使用jQuery ajax传递对象的复杂阵列
例子:
[{"Item":{"ClientId":"2","Projects":[{"ProjectId":"81","TypeId":"8"}],"Types":[{"ProjectType":"2","ProjectName":"yty - موقع2 - aaaaaaaa"},{"ProjectType":"6","ProjectName":"yty - موقع - aaaaaaaa"}]}},{"Item":{"ClientId":"7","Projects":[{"ProjectId":"75","TypeId":"8"},{"ProjectId":"76","TypeId":"8"}],"Types":[{"ProjectType":"2","ProjectName":"mona - موقع2 - aaaaaaaa"}]}}]
jQuery代码:
$("#btnSave").click(function() {
var array = [];
var donatorId = $('.ddlDonators').val();
$.each($('.cbxClient:checked'), function() {
var obj = new Object();
var item = new Object();
var projectArray = [];
var projTypeArray = [];
var attrId = $(this).attr('id');
var clientId = attrId.split('_')[1];
$.each($('.formPnl').find('.' + attrId).find('li .Project:checked'), function() {
var projObj = new Object();
projObj.ProjectId = $(this).val();
projObj.TypeId = $(this).attr('ProjectType');
projectArray.push(projObj);
});
$.each($('.formPnl').find('.' + attrId).find('li .ProjectType:checked'), function() {
var projTypeObj = new Object();
projTypeObj.ProjectType = $(this).val();
projTypeObj.ProjectName = $(this).attr('name') + " - " + $('.ddlDonators option:checked').text();
projTypeArray.push(projTypeObj);
});
obj.ClientId = attrId.split('_')[1];
obj.CProject = projectArray;
obj.Types = projTypeArray;
item.Item = obj;
array.push(item);
//ProjectType = $('.formPnl').find('.' + attrId).find('li .ProjectType:checked').val();
//ProjectName = $('.formPnl').find('.' + attrId).find('li .ProjectType:checked').attr('name') + " - " + $('.ddlDonators option:checked').text();
//ProjectId = $('.formPnl').find('.' + attrId).find('li .Project:checked').val();
//ProjectType = $('.formPnl').find('.' + attrId).find('li .Project:checked').attr('ProjectType');
});
$.ajax({
type: "POST",
url: "Default.aspx/SaveProjects",
traditional :true,
data: "{'DonatorId':'" + donatorId + "','Items':'" + JSON.stringify(array) + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
},
error: function (data) {
alert('error')
// alert(data.responseText)
}
}); // end of ajax
console.log(JSON.stringify(array));
});
C#代码:
[WebMethod]
public static void SaveProjects(int DonatorId, List<Item> Items)
{
}
public class Item
{
public int ClientId { get; set; }
public List<CProject> Projects { get; set; }
public List<Type> Types { get; set; }
}
public class CProject
{
public int ProjectId { get; set; }
public int TypeId { get; set; }
}
public class Types
{
public int ProjectType { get; set; }
public string ProjectName { get; set; }
}
的问题是,在调试时我发现的项目数为2,但它里面的道具= null!
在此先感谢
你由内而外托的意思,如果您发布的调试 – 2014-11-25 11:40:51
@Soner格尼尔的结果会更好,我的意思是项目的数量为2 whic是正确的,但是这些属性的值是空的,例如ClientId = 0,其他则为空! – user2120121 2014-11-25 11:51:47