首页 > 解决方案 > 使用 WebMethod 从数组中获取值

问题描述

我有以下 jquery 数组,它从表中的两列中获取值并显示它们的值:

$(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);
                     }
                 });
             });
         });

第一列的名称是“标签”,第二列的名称是控制台中数组的“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) 采用 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# 中的值。

提前致谢 !

标签: c#webformswebmethod

解决方案


确保您的 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);
                 }
             });

创建一个具有两个属性的模型类: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";
}

谢谢。


推荐阅读