如何从c#中的对象获取值

时间:2015-02-05 09:03:39

标签: c# asp.net ajax json json.net

我的Json代码是

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

}

如何从Object orderDetails获取值。我不能使用索引。

2 个答案:

答案 0 :(得分:3)

首先需要创建订单明细对象:

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

然后将您的网络方法更改为:

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

}

取而代之的是List<OrderDetails>

答案 1 :(得分:0)

您可以使用反射:

foreach(var order in orderDetails)
{
    string orderType = (string)order.GetType().GetProperty("OrderType").GetValue(order);
    // other properties
}
相关问题