I have a post method in Web API ASP.NET that accepts a single Json object. Here is the code with a code to check if data from the posting Json is existing in my database. and I will be using Linq to interact with my DB. Sales_ was auto-generated in my context, I believe from EntityFramework. db is my database object.
public HttpResponseMessage PostSales(Sales Sales, [FromUri] string auth)
{
try
{
if (ModelState.IsValid)
{
if (auth == "KDI")
{
Int64 rs = db.Sales_.Where(sl => sl.Serial == Sales.Serial).Count();
if (rs == 1)
{
return Request.CreateErrorResponse(HttpStatusCode.Conflict, " Duplicate Found!");
}
else
{
db.Sales_.Add(Sales);
db.SaveChanges();
return Request.CreateErrorResponse(HttpStatusCode.OK, "Added!");
}
//Closing elses cut out for this question.
That is working fine and it accepts my Json fine as a single object.
Here is my Json that will be posted.
{
"Serial": 1,
"ExtSerial": "AH0000002",
"Date": "2015-03-01",
"CustomerRefNbr": "JPM0001",
"Description": "2015 FEBRUARY RENTAL 2015 FEBRUARY RENTAL",
"Customer": "TRDE0065",
"Amount": 17989.51,
"AQ_Branch": "KDI",
"AQ_COA": "4100503000",
"LineSubAccount": "OPMOPN000",
"LineTaxCategory": "JPMTAX",
"LineQuantity": 1,
"LineUnitPrice": 400000,
"AQ_PostStatus": 1,
"AQ_StatusDate": "2015-03-01",
"DTS": "2015-03-01",
"LineDescription": "Line Description"
}
Now, I wanted to post multiple lists or lines (not sure what to call them) in a json object. Something like this:
{
"ExtSerial": "AH0000002",
"Date": "2015-03-01",
"CustomerRefNbr": "JPM0001",
"Description": "2015 FEBRUARY RENTAL 2015 FEBRUARY RENTAL",
"Customer": "TRDE0065",
"Amount": 17989.51,
"AQ_Branch": "KDI",
"AQ_COA": "4100503000",
"LineSubAccount": "OPMOPN000",
"LineTaxCategory": "JPMTAX",
"LineQuantity": 1,
"LineUnitPrice": 400000,
"AQ_PostStatus": 1,
"AQ_StatusDate": "2015-03-01",
"DTS": "2015-03-01",
"LineDescription": "Line Description"
},{
"ExtSerial": "AH0000003",
"Date": "2015-04-01",
"CustomerRefNbr": "JPM0002",
"Description": "2015 FEBRUARY RENTAL 2015 FEBRUARY RENTAL",
"Customer": "TRDE0066",
" Amount": 17989.51,
"AQ_Branch": "KDI",
"AQ_COA": "4100503000",
"LineSubAccount": "OPMOPN000",
"LineTaxCategory": "JPMTAX",
"LineQuantity": 2,
"LineUnitPrice": 400000,
"AQ_PostStatus": 1,
"AQ_StatusDate": "2015-04-01",
"DTS": "2015-04-01",
"LineDescription": "Line Description"
}
Not sure how it will be read on my post method though. I have tried changing my parameter Sales to List<Sales> Sales
. I think that is what to use for a list in an object but my problem is that my db.Sales_.Add(Sales);
is producing an error now. (invalid arguments) Not sure how to use the .Add() with a list. Also, I can't get the data of my Sales table column serial as shown in Int64 rs = db.Sales_.Where(sl => sl.Serial == Sales.Serial).Count();
I'm pretty much stuck and don't know how to do this. I hope you don't find my question stupid. I just need your help. I know it's a simple fix.