How can I post multiple json objects in web api?

asp.net-web-api entity-framework-6 json linq

Question

I have a web-api controller that accepts a single Json object Post (currently using Postman as my user-agent).

Here is my Post method:

    private AH_ODS_DB_Entities db = new AH_ODS_DB_Entities();    
    [ResponseType(typeof(Sales))]
            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 (1 == rs)
                            {
                                return Request.CreateErrorResponse(HttpStatusCode.Conflict, " Duplicate Found!");
                            }
                            else
                            {
                                db.Sales_.Add(Sales);
                                db.SaveChanges();
                                return Request.CreateErrorResponse(HttpStatusCode.OK, "Added!");
                            }
                        }
                        else
                        {
                            return Request.CreateResponse(HttpStatusCode.Unauthorized, "Unauthorized Access!");
                        }
                    }
                    else
                    {


  return Request.CreateResponse(HttpStatusCode.InternalServerError, "Something's wrong with the JSON model you sent me.");
                }
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
            }

        }

And here is the Json:

{
  "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"
}

It works fine and all but I wanted to have my post method to optionally accept multiple array of Json data

Something like this:

[{"Id1":3,"Id2":76,"Id3":19},{"Id1":56,"Id2":87,"Id3":94},{"Id1":976,"Id2":345,"Id3":7554}]

I tried putting my sales model parameter as a list<> but it could not read my Sales entity db.Sales_.Add(Sales); and it could not get the table serial in my Sales entity in Int64 rs = db.Sales_.Where(sl => sl.Serial == Sales.Serial).Count();

I am using EF6 and LINQ btw. I really need your help, I know it is a simple problem but I'm really stuck.

1
1
4/30/2015 6:38:16 AM

Popular Answer

You cannot receive multiple JSON objects in a Web API POST action. That's because it can only deserialize the POST payload once.

The only solution is to post (client side) an receive (web action parameter) an object which has the other objects as properties.

Your question can not be very well understodd, however, I'll try to give you a more concrete answer. You need to define a class like this as your web api parameter:

public class SalesAndIds
{
    public Sales Sales { get; set; }
    public List<Ids> IdsList { get; set; }
    public class Ids
    {
        public int Id1 { get; set; }
        public int Id2 { get; set; }
        public int Id3 { get; set; }
    }
}

And the object that you post from JavaScript must look like this:

{
   Sales: { /* the sales object*/ },
   IdsList: [ /* the array with the ids list */  ]
}
2
4/30/2015 8:06:02 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow