在c#中获取对象数组的键和值

时间:2016-11-28 06:32:40

标签: c# arrays ajax

伙计我正在获取对象数组中的值并从ajax中访问web方法但我无法获取该数组的键和值。下面是我的ajax代码

var ShedulerTableCount = $('#dvSave table').length;
            var ShedulersData = [];
            ShedularCount = new Object({ ShedulerCount: $('#txtTimeSeries').val() })
            ShedulersData.push(ShedularCount);
            for(var k=1; k <= ShedulerTableCount ; k++)
            {
                var tableId = $('#tblTimeInterval' + k);
                tableId.find('tbody tr').each(function (i, el) {
                    Shedularobj = new Object({ ShedularName:'',InterVal: '', TimeSeries: '', LockingPeriod: '', TradeDuration: '', Min: '', Max: '', Touch: '', NoTouch: '', IPG: '', IPGDeduction: '', Closeable: '', txtTimeSeries: '', txtGap: '', txtRollingInterval:'' });
                    var $tds = $(this).find('td');
                    Shedularobj.ShedularName = 'sheduler ' + k;
                    Shedularobj.InterVal = $tds.find('label').eq(0).text();
                    Shedularobj.TimeSeries = $tds.find('label').eq(1).text();
                    Shedularobj.LockingPeriod = $tds.find('input').eq(0).val();
                    Shedularobj.TradeDuration = $tds.find('input').eq(1).val();
                    Shedularobj.Min = $tds.find('input').eq(2).val();
                    Shedularobj.Max = $tds.find('input').eq(3).val();
                    Shedularobj.Touch = $tds.find('input').eq(4).val();
                    Shedularobj.NoTouch = $tds.find('input').eq(5).val();
                    Shedularobj.IPG = $tds.find('input').eq(6).val();
                    Shedularobj.IPGDeduction = $tds.find('input').eq(7).val();
                    Shedularobj.Closeable = $tds.find('input').eq(8).val();
                    Shedularobj.txtTimeSeries = $('#txtTimeSeries').val();
                    Shedularobj.txtGap = $('#txtGap').val();
                    Shedularobj.txtRollingInterval = $('#txtRollingInterval').val();
                    ShedulersData.push(Shedularobj);

                });
            }

      $.ajax({
                    type: "POST",
                    url: "BinarySetting.aspx/SaveShedulers",
                    data: "{ShedulersData:" + JSON.stringify(ShedulersData) + "}",
                    contentType: 'application/json; charset=utf-8',
                    success: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert('Success');
                    },
                    error: function (result) {
                        alert('error');
                    }
                });

这是我想要获取对象的键和值的c#代码:

 [WebMethod]
    public static string SaveShedulers(object[] ShedulersData)
    {
        object objCount = ShedulersData[0];
       // System.Reflection.PropertyInfo pi = objCount.GetType().GetProperty("Value");
        var ss =  objCount.GetType().GetProperties().Single(pi => pi.Name == "ShedularCount").GetValue(objCount, null);
        return "ss";
    }

我还将附上截图以更清楚地了解情景:

enter image description here

2 个答案:

答案 0 :(得分:1)

在您的代码中创建一个类,如此

public class SchedulerTuple
{
   public string ShedularName { get; set; }
   public string InterVal { get; set; }
   public string TimeSeries { get; set; }
   public string LockingPeriod { get; set; }
   public string TradeDuration { get; set; }
   public string Min { get; set; }
   public string Max { get; set; }
   public string Touch { get; set; }
   public string NoTouch { get; set; }
   public string IPG { get; set; }
   public string IPGDeduction { get; set; }
   public string Closeable { get; set; }
   public string txtTimeSeries { get; set; }
   public string txtGap { get; set; }
   public string txtRollingInterval { get; set; }
}

然后将您的webmethod的参数更改为像这样的列表

[WebMethod]
public static string SaveShedulers(List<SchedulerTuple> ShedulersData)
{
    // your code here
}

如果你想像调度程序计数一样将其他值传递给你的代码,那么添加另一个参数而不是在一个对象中包含很多东西。

我认为你不需要在这里使用反射。我希望它有所帮助。

答案 1 :(得分:0)

除了@Danish的想法,修改这样的ajax调用。

   $.ajax({
                        type: "POST",
                        url: "BinarySetting.aspx/SaveShedulers",
                        data: ShedulersData, //<-------------
                        contentType: 'application/json; charset=utf-8',
                        success: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert('Success');
                        },
                        error: function (result) {
                            alert('error');
                        }
                    });