C# WebForms-使用jQueryAjax传递复杂的对象数组
我想传递一个复杂的对象数组,evrey对象还有另外两个对象数组 例如:C# WebForms-使用jQueryAjax传递复杂的对象数组,c#,jquery,asp.net,arrays,ajax,C#,Jquery,Asp.net,Arrays,Ajax,我想传递一个复杂的对象数组,evrey对象还有另外两个对象数组 例如: [{"Item":{"ClientId":"2","Projects":[{"ProjectId":"81","TypeId":"8"}],"Types":[{"ProjectType":"2","ProjectName":"yty - موقع2 - aaaaaaaa"},{"ProjectType":"6","ProjectName":"yty - موقع - aaaaaaaa"}]}},{"Item":{"Clie
[{"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,但其中的prop=null
提前谢谢我已经做了这件事
首先,您必须更改将列表转换为字符串的参数,因为您要通过stringify将json数组传递到字符串中
然后反序列化对象主数组并通过以下代码查找嵌套数组
[WebMethod]
public static void SaveProjects(int DonatorId, String MainArray)
{
Dictionary<string, dynamic> mainDictionary = (Dictionary<string, dynamic>)deserializer.DeserializeObject(MainArray);
Dictionary<string, object> masterData = mainDictionary["SubArray"] as Dictionary<string, object>;
object[] objNestedarray= masterData ["NestedArrayName"] as object[];
}
希望以上代码能帮助您得到解决方案 实际上是我的错,客户端的数组结构与服务器端不一样,所以映射失败,所以我只是在客户端对item对象进行了注释
$("#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(obj);
});
$.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));
});
服务器端:
[WebMethod]
public static void SaveProjects(int DonatorId, List<Item> Items)
{
}
public class Item
{
public int ClientId { get; set; }
public List<CProject> CProject { get; set; }
public List<Types> 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; }
}
你说的道具在里面是什么意思,如果你把你的结果贴出来会更好debug@SonerGönül,我的意思是项目的计数是2,这是正确的,但是属性的值是空的,例如ClientId=0,其他的为null!!!