vishu minhas vishu minhas - 7 days ago 5
C# Question

Get key and value of object array in c#

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

Answer

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.

Comments