你好,我有一点问题。
我正在向WebMethod发送一些值,我需要两个列表,并且要读取一个foreach(oneCollection和myCollection)。
现在只读取第一个列表(oneCollection),但第二个列表不读取。 我认为这行代码有问题(我认为这不是一次读取两个列表的正确方法。):
public static void GetCollection(List<oneCollection> uoneCollection, List<myCollection> omyCollection)
整个代码:
public class oneCollection
{
public String lvl { get; set; }
public String ddl { get; set; }
}
public class myCollection
{
public String label { get; set; }
public String opis { get; set; }
}
[WebMethod(EnableSession = true)]
public static void GetCollection(List<oneCollection> uoneCollection, List<myCollection> omyCollection)
{
DataTable tabela2 = new DataTable();
tabela2.Columns.Add("BusinessCenterID");
tabela2.Columns.Add("ObjekatName");
foreach (oneCollection firstcol in uoneCollection)
{
string lvl = firstcol.lvl;
string ddl = firstcol.ddl;
tabela2.Rows.Add(lvl, ddl);
}
DataTable tabela = new DataTable();
tabela.Columns.Add("CharacteristicID");
tabela.Columns.Add("CharacteristicValue");
foreach (myCollection mycol in omyCollection)
{
string label = mycol.label;
string opis = mycol.opis;
tabela.Rows.Add(label, opis);
}
return;
}
以下是调用此方法的javascript / jQuery代码:
$(function () {
$('#myButton').on('click', function () {
var oneCollection = [];
oneCollection.push({
lvl: $('#MainContent_txtProductConstruction').val(),
ddl: $('#MainContent_ddlBusinessCenter').val(),
})
var myCollection = [];
$('#MainContent_gvKarakteristike tbody').find('tr:gt(0)').each(function (i, e) {
var row = $(e);
myCollection.push({
label: valuefromType(row.find(row.find('td:eq(1)').children())),
opis: valuefromType(row.find(row.find('td:eq(2)').children()))
});
});
console.log(myCollection);
console.log(oneCollection);
function valuefromType(control) {
var type = $(control).prop('nodeName').toLowerCase();
switch (type) {
case "input":
return $(control).val();
case "span":
return $(control).text();
case "select":
return $(control).val();
}
}
$.ajax({
type: "POST",
url: "FirstPage.aspx/GetCollection",
data: JSON.stringify({ 'uoneCollection': oneCollection, 'omyCollection': myCollection}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert("Saved successfully.");
console.log(response);
},
error: function (response) {
console.log(response);
}
});
//location.reload();
});