[英]How to get values from object in c#
my Json code is 我的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 is 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 the Object orderDetails. 如何从Object orderDetails获取值。 I cant use indexing. 我不能使用索引。
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: 然后将您的Web方法更改为:
[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. 它接受List<OrderDetails>
。
You can use reflection: 你可以使用反射:
foreach(var order in orderDetails)
{
string orderType = (string)order.GetType().GetProperty("OrderType").GetValue(order);
// other properties
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.