Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get values from object in c#

my Json code is

ListOrderDetails.push({ // Add Order Details to array                  
                "OrderType": OrderType,
                "CaseNumber": CaseNumber,
                "OrderNumber": OrderNumber,
                "OrderStatus": OrderStatus,
                "Reason": Reason,
                "Coments": Coments
            });

var Params = { "Geo": Geography, "GeoId": GeographyID, "CountryCode": CountryCode, "Segment": Segment, "SubsegmentID": SubSegmentID, "OrderDetails": ListOrderDetails };
        //var Params = { "Geo": Geography, "GeoId": GeographyID, "CountryCode": CountryCode, "Segment": Segment, "SubsegmentID": SubSegmentID };
        $.ajax({
            type: "POST",
            url: "MyDataVer1.aspx/SaveManualEntry",
            contentType: "application/json",
            data: JSON.stringify(Params),
            dataType: "json",
            success: function(response) {
                alert(response);
            },
            error: function(xhr, textStatus, errorThrown) {
                alert("xhr : " + xhr);
                alert("textStatus : " + textStatus);
                alert("errorThrown " + errorThrown);
            }
        });

c# webmethod is

[WebMethod]
public static int SaveManualEntry(string Geo, int GeoId, string CountryCode,
                                  string Segment, string SubsegmentID, 
                                  object[] OrderDetails)
{

    try
    {
        int TotalOrderCount = 0;
        int Successcount = 0;               
        return Successcount;

    }
    catch (Exception ex)
    {
        throw ex;
    }

}

How to get values from the Object orderDetails. I cant use indexing.

like image 571
Ramesh Avatar asked Feb 05 '15 09:02

Ramesh


People also ask

What does object values do?

Object.values() returns an array whose elements are the enumerable property values found on the object. The ordering of the properties is the same as that given by looping over the property values of the object manually.

How to get values from object array in JavaScript?

To get the values of an object as an array, use the Object. values() method, passing it the object as a parameter. The method returns an array containing the object's property values in the same order as provided by a for ... in loop. Copied!


2 Answers

You can use reflection:

foreach(var order in orderDetails)
{
    string orderType = (string)order.GetType().GetProperty("OrderType").GetValue(order);
    // other properties
}
like image 67
MacGyver Avatar answered Oct 13 '22 20:10

MacGyver


You first need to create an order detail object:

public class OrderDetail
{
    public string OrderType { get; set; }
    public string CaseNumber { get; set; }
    public string OrderNumber { get; set; }
    public string OrderStatus { get; set; }
    public string Reason { get; set; }
    public string Coments { get; set; }
}

Then change your web method to this:

[WebMethod]
public static int SaveManualEntry(string Geo, int GeoId, string CountryCode,
                                  string Segment, string SubsegmentID, 
                                  List<OrderDetail> OrderDetails)
{

    try
    {
        int TotalOrderCount = 0;
        int Successcount = 0;               
        return Successcount;

    }
    catch (Exception ex)
    {
        throw ex;
    }

}

Which accepts a List<OrderDetails> instead.

like image 40
hutchonoid Avatar answered Oct 13 '22 22:10

hutchonoid