使用 WebMethod 从数组中获取值
Take the values from the array with WebMethod
我有以下 jquery 数组,它从我的 table 的两列中获取值并显示它们的值:
$(function () {
$('#myButton').on('click', function () {
var myCollection = [];
$('#MainContent_gvKarakteristike tbody').find('tr:gt(0)').each(function () {
var row = this;
var myObj = {
label: valuefromType($(row).find($(row).find('td:eq(1)').children())),
opis: valuefromType($(row).find($(row).find('td:eq(2)').children()))
};
myCollection[myCollection.length] = myObj;
});
console.log(myCollection)
function valuefromType(control) {
var type = $(control).prop('nodeName').toLowerCase();
switch (type) {
case "input":
return $(control).val();
break;
case "span":
return $(control).text();
break;
case "select":
return $(control).val();
break;
}
}
$.ajax({
type: "POST",
url: "FirstPage.aspx",
//data: { obj: myCollection },
data: JSON.stringify(myCollection),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
console.log(response);
},
error: function (response) {
console.log(response);
}
});
});
});
第一列的名称是 'label',第二列的名称是 'opis' 控制台中数组的结果:
myCollection
(6) […]
0: Object { label: "1", opis: "Test1" }
1: Object { label: "2", opis: "Test2" }
2: Object { label: "3", opis: "Test3" }
3: Object { label: "5", opis: "1" }
4: Object { label: "9", opis: "Test5" }
5: Object { label: "15", opis: "Test6" }
length: 6
单击此按钮获取值:
<button id="myButton" type="button">Save</button>
Ajax(status 200) 正确取值 POST
JSON:
0 {…}
label 1
opis test1
1 {…}
label 2
opis test2
2 {…}
label 3
opis test3
3 {…}
label 5
opis 1
4 {…}
label 9
opis test5
5 {…}
label 15
opis test6
有人可以帮我解决 C# 部分吗?
我需要 WebMethod 从 ajax 中获取值,这样我就可以将它发送到数据库,或者只是帮助我读取 c# 中的值。
提前致谢!
确保您的ajax在数据
中获得了参数和值
$.ajax({
type: "POST",
url: "FirstPage.aspx",
data: JSON.stringify({'omyCollection': myCollection}), // Check this call.
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
console.log(response);
},
error: function (response) {
console.log(response);
}
});
创建一个具有两个属性的模型 class:label,opis
注意:属性应与 ajax 数组的属性同名。
public class myCollection
{
public String label{ get; set; }
public String opis{ get; set; }
}
现在使用 List< myCollection > 类型的参数在代码隐藏中创建一个 WebMethod:
[WebMethod(EnableSession = true)]
public static string GetCollection(List<myCollection> omyCollection)
{
foreach (myCollection mycol in omyCollection)
{
string id = mycol.label; //access label from myCol object
string opis = mycol.opis;
//do something
}
return "response";
}
谢谢。
我有以下 jquery 数组,它从我的 table 的两列中获取值并显示它们的值:
$(function () {
$('#myButton').on('click', function () {
var myCollection = [];
$('#MainContent_gvKarakteristike tbody').find('tr:gt(0)').each(function () {
var row = this;
var myObj = {
label: valuefromType($(row).find($(row).find('td:eq(1)').children())),
opis: valuefromType($(row).find($(row).find('td:eq(2)').children()))
};
myCollection[myCollection.length] = myObj;
});
console.log(myCollection)
function valuefromType(control) {
var type = $(control).prop('nodeName').toLowerCase();
switch (type) {
case "input":
return $(control).val();
break;
case "span":
return $(control).text();
break;
case "select":
return $(control).val();
break;
}
}
$.ajax({
type: "POST",
url: "FirstPage.aspx",
//data: { obj: myCollection },
data: JSON.stringify(myCollection),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
console.log(response);
},
error: function (response) {
console.log(response);
}
});
});
});
第一列的名称是 'label',第二列的名称是 'opis' 控制台中数组的结果:
myCollection
(6) […]
0: Object { label: "1", opis: "Test1" }
1: Object { label: "2", opis: "Test2" }
2: Object { label: "3", opis: "Test3" }
3: Object { label: "5", opis: "1" }
4: Object { label: "9", opis: "Test5" }
5: Object { label: "15", opis: "Test6" }
length: 6
单击此按钮获取值:
<button id="myButton" type="button">Save</button>
Ajax(status 200) 正确取值 POST JSON:
0 {…}
label 1
opis test1
1 {…}
label 2
opis test2
2 {…}
label 3
opis test3
3 {…}
label 5
opis 1
4 {…}
label 9
opis test5
5 {…}
label 15
opis test6
有人可以帮我解决 C# 部分吗? 我需要 WebMethod 从 ajax 中获取值,这样我就可以将它发送到数据库,或者只是帮助我读取 c# 中的值。
提前致谢!
确保您的ajax在数据
中获得了参数和值 $.ajax({
type: "POST",
url: "FirstPage.aspx",
data: JSON.stringify({'omyCollection': myCollection}), // Check this call.
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
console.log(response);
},
error: function (response) {
console.log(response);
}
});
创建一个具有两个属性的模型 class:label,opis 注意:属性应与 ajax 数组的属性同名。
public class myCollection
{
public String label{ get; set; }
public String opis{ get; set; }
}
现在使用 List< myCollection > 类型的参数在代码隐藏中创建一个 WebMethod:
[WebMethod(EnableSession = true)]
public static string GetCollection(List<myCollection> omyCollection)
{
foreach (myCollection mycol in omyCollection)
{
string id = mycol.label; //access label from myCol object
string opis = mycol.opis;
//do something
}
return "response";
}
谢谢。