0

Guys I am taking the values in array of object and hitting the webmethod from ajax but I am not able to get the key and values of that array. Below is my ajax code

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');
                    }
                });

And this is my c# code from where I want to fetch the key and value of the object:

 [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";
    }

I am also going to attach the screenshot to understand the scenario more clearly:

enter image description here

8
  • How about ShedulersData[0].Key and ShedulersData[0].Value Commented Nov 28, 2016 at 6:34
  • how you fill data in ShedulersData in client side? Commented Nov 28, 2016 at 6:35
  • @MohitShrivastava there is not any key property in this otherwise I can easily get this have only methods in it that is GetType() Commented Nov 28, 2016 at 6:36
  • @SURJEETSINGHBisht I have update my question to show how I am filling the data in ShedulersData Commented Nov 28, 2016 at 6:37
  • Cast your object to a class type that has Key and Value properties and that should solve it. Commented Nov 28, 2016 at 6:38

2 Answers 2

1

Create a class in your code behind like this

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; }
}

and then change the paramaters of your webmethod to a List like this

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

if you want to pass other values to your code behind like the scheduler count, then add another argument instead of packing a lot of stuff in a single object.

I don't think you need to use reflection here. I hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

0

In addition to @Danish ideas, modify the ajax call like this.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.