Reputation: 58
new to ASP.net and have a question involving a model that has multiple variables of the same type.
I have Order model that can have 0 to n Detail models attached to it. When I POST the http request I get all the information for the Order except the Detail model always comes back as null. Or more specifically the ".count" method on the List is 0.
How can I get it to post all the Details for my Model?
Order Model:
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>();
}
}
Detail Model:
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";
}
}
}
Repository class:
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;
}
}
}
And my Controller:
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;
}
}
}
This is the XML data being requested:
<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>
And this is my current response:
<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>
I also have no control over changing the format of the incoming xml data. Any pointer in the right direction would be helpful please.
Debug:
[debug] (https://i.sstatic.net/OTkoy.jpg)
Upvotes: 0
Views: 146
Reputation: 58
Okay,
So I solved this by copying the XML data onto my clipboard, creating a new class and then going to Edit -> Paste Special -> Paste XML as classes
and it came up with this:
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;
}
}
}
Just in case anyone ever stumbles across this problem. What was giving me hours of headache took 3 clicks to solve... Thanks all for your replies.
Upvotes: 1