ASP.net的新手,并且有一个涉及具有多个相同类型变量的模型的问题。
我有订单模型,可以有0到n附加的详细模型。当我发布http请求时,我获得了Order的所有信息,除了Detail模型总是返回null。或者更具体地说,List上的“.count”方法是0。
如何让它发布我的模型的所有详细信息?
订单型号:
public struct shipTo
{
public string id;
public string Line1;
public int PostalCode;
public string City;
public string State;
public string CountryCode;
}
namespace USS_EDIv2.Models
{
public class Order
{
public Int64 SalesOrderNumber { get; set; }
public Int64 PurchaseOrderNumber { get; set; }
public Int64 BranchPlant { get; set; }
public shipTo ShipTo;
public Int64 Quantity { get; set; }
public string UOM { get; set; }
public List<Detail> Detail = new List<Detail>();
}
}
详细模型:
namespace USS_EDIv2.Models
{
public class Detail
{
public string LineNumber;
public string GradeItem;
public string Quantity;
public string UOM;
public string RequestDate;
public string Status;
public Detail()
{
LineNumber = "1";
GradeItem = "1";
Quantity = "1";
UOM = "1";
RequestDate = "1";
Status = "1";
}
}
}
存储库类:
namespace USS_EDIv2.Services
{
public class SalesRepository
{
public DateTime currentTime = System.DateTime.Now;
private const string CacheKey = "4041tmtTEST1337";
public string xml;
public int i;
public Order[] GetAllSales()
{
var ctx = HttpContext.Current;
if (ctx != null)
{
return (Order[])ctx.Cache[CacheKey];
}
return new Order[]
{
new Order
{
}
};
}
public SalesRepository()
{
var ctx = HttpContext.Current;
if (ctx != null)
{
if (ctx.Cache[CacheKey] == null)
{
var sales = new Order[]
{
};
{
};
ctx.Cache[CacheKey] = sales;
//ctx.Cache.Remove("4041tmtTEST1337");
}
}
}
public bool SaveSale(Order sales)
{
var ctx = HttpContext.Current;
if (ctx != null)
{
try
{
var currentData = ((Order[])ctx.Cache[CacheKey]).ToList();
currentData.Add(sales);
ctx.Cache[CacheKey] = currentData.ToArray();
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return false;
}
}
return false;
}
}
}
我的控制器:
namespace USS_EDIv2.Controllers
{
public class NewOrderController : ApiController
{
public DateTime currentTime = System.DateTime.Now;
private SalesRepository salesRepository;
public int i;
public NewOrderController()
{
this.salesRepository = new SalesRepository();
}
public Order[] Get()
{
return salesRepository.GetAllSales();
}
public HttpResponseMessage Post(Order sale)
{
var ctx = HttpContext.Current;
this.salesRepository.SaveSale(sale);
var response = Request.CreateResponse<Order> (System.Net.HttpStatusCode.Created, sale);
return response;
}
}
}
这是要求的XML数据:
<Order>
<SalesOrderNumber>1294288</SalesOrderNumber>
<PurchaseOrderNumber>81896</PurchaseOrderNumber>
<BranchPlant>9701</BranchPlant>
<ShipTo id="string">
<Line1>RAIL TRACK #769 SPOT 00</Line1>
<PostalCode>79765</PostalCode>
<City>MIDLAND COUNTY</City>
<State>TX</State>
<CountryCode>US</CountryCode>
</ShipTo>
<Quantity>75</Quantity>
<UOM>TN</UOM>
<Detail created="2015-12-14T13:59:57.84" action="Create">
<LineNumber>1.0</LineNumber>
<GradeItem>97010B00000</GradeItem>
<Quantity>25.000</Quantity>
<UOM>TN</UOM>
<RequestDate>2015-07-11</RequestDate>
<Status>Open</Status>
</Detail>
<Detail created="2015-12-14T13:59:57.84" action="Create">
<LineNumber>2.0</LineNumber>
<GradeItem>97010B00000</GradeItem>
<Quantity>25</Quantity>
<UOM>TN</UOM>
<RequestDate>2002-02-07</RequestDate>
<Status>Open</Status>
</Detail>
<Detail created="2015-05-22T02:29:50.78" action="Create">
<LineNumber>3.0</LineNumber>
<GradeItem>97010B00000</GradeItem>
<Quantity>25</Quantity>
<UOM>TN</UOM>
<RequestDate>2015-07-11</RequestDate>
<Status>Open</Status>
</Detail>
</Order>
这是我目前的回应:
<ArrayOfOrder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Order>
<ShipTo>
<Line1>RAIL TRACK #769 SPOT 00</Line1>
<PostalCode>79765</PostalCode>
<City>MIDLAND COUNTY</City>
<State>TX</State>
<CountryCode>US</CountryCode>
</ShipTo>
************HERE LIES THE PROBLEM
<Detail/>
************
<SalesOrderNumber>1294288</SalesOrderNumber>
<PurchaseOrderNumber>81896</PurchaseOrderNumber>
<BranchPlant>9701</BranchPlant>
<Quantity>75</Quantity>
<UOM>TN</UOM>
</Order>
</ArrayOfOrder>
我也无法控制更改传入的xml数据的格式。正确方向的任何指针都会有所帮助。
调试:
[debug](http://imgur.com/RVnpZ0S)
答案 0 :(得分:1)
好的,
所以我通过将XML数据复制到剪贴板上,创建了一个新类然后转到Edit - &gt;来解决这个问题。选择性粘贴 - &gt;将XML粘贴为类
它想出了这个:
namespace USS_EDIv2.Models
{
public class Orders
{
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class Order
{
private uint salesOrderNumberField;
private uint purchaseOrderNumberField;
private ushort branchPlantField;
private OrderShipTo shipToField;
private byte quantityField;
private string uOMField;
private OrderDetail[] detailField;
/// <remarks/>
public uint SalesOrderNumber
{
get
{
return this.salesOrderNumberField;
}
set
{
this.salesOrderNumberField = value;
}
}
/// <remarks/>
public uint PurchaseOrderNumber
{
get
{
return this.purchaseOrderNumberField;
}
set
{
this.purchaseOrderNumberField = value;
}
}
/// <remarks/>
public ushort BranchPlant
{
get
{
return this.branchPlantField;
}
set
{
this.branchPlantField = value;
}
}
/// <remarks/>
public OrderShipTo ShipTo
{
get
{
return this.shipToField;
}
set
{
this.shipToField = value;
}
}
/// <remarks/>
public byte Quantity
{
get
{
return this.quantityField;
}
set
{
this.quantityField = value;
}
}
/// <remarks/>
public string UOM
{
get
{
return this.uOMField;
}
set
{
this.uOMField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Detail")]
public OrderDetail[] Detail
{
get
{
return this.detailField;
}
set
{
this.detailField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class OrderShipTo
{
private string line1Field;
private uint postalCodeField;
private string cityField;
private string stateField;
private string countryCodeField;
private string idField;
/// <remarks/>
public string Line1
{
get
{
return this.line1Field;
}
set
{
this.line1Field = value;
}
}
/// <remarks/>
public uint PostalCode
{
get
{
return this.postalCodeField;
}
set
{
this.postalCodeField = value;
}
}
/// <remarks/>
public string City
{
get
{
return this.cityField;
}
set
{
this.cityField = value;
}
}
/// <remarks/>
public string State
{
get
{
return this.stateField;
}
set
{
this.stateField = value;
}
}
/// <remarks/>
public string CountryCode
{
get
{
return this.countryCodeField;
}
set
{
this.countryCodeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string id
{
get
{
return this.idField;
}
set
{
this.idField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class OrderDetail
{
private decimal lineNumberField;
private string gradeItemField;
private decimal quantityField;
private string uOMField;
private System.DateTime requestDateField;
private string statusField;
private System.DateTime createdField;
private string actionField;
/// <remarks/>
public decimal LineNumber
{
get
{
return this.lineNumberField;
}
set
{
this.lineNumberField = value;
}
}
/// <remarks/>
public string GradeItem
{
get
{
return this.gradeItemField;
}
set
{
this.gradeItemField = value;
}
}
/// <remarks/>
public decimal Quantity
{
get
{
return this.quantityField;
}
set
{
this.quantityField = value;
}
}
/// <remarks/>
public string UOM
{
get
{
return this.uOMField;
}
set
{
this.uOMField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType = "date")]
public System.DateTime RequestDate
{
get
{
return this.requestDateField;
}
set
{
this.requestDateField = value;
}
}
/// <remarks/>
public string Status
{
get
{
return this.statusField;
}
set
{
this.statusField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public System.DateTime created
{
get
{
return this.createdField;
}
set
{
this.createdField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string action
{
get
{
return this.actionField;
}
set
{
this.actionField = value;
}
}
}
以防万一有人偶然发现这个问题。什么给了我几个小时的头痛需要3次点击解决...感谢所有的回复。