我想傳遞一個複雜的對象數組,並且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