How to get values ​​from an object in C #

my json code

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

[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 an OrderDetails object. I can not use indexing.

+4
source share
2 answers

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

}

It accepts instead List<OrderDetails>.

+3
source

You can use reflection:

foreach(var order in orderDetails)
{
    string orderType = (string)order.GetType().GetProperty("OrderType").GetValue(order);
    // other properties
}
0
source

All Articles