0

I have a web-api, 2 tables in my SQL DB, JT and Sales. Before I add() to the database I need the poster to specify first in the uri whether he/she wants to post to JT table or Sales. My problem is that my post method only accepts one model binding, it doesn't want two like as shown on my code below. It doesn't have errors but when I post with that logic in mind it returns an error in POSTMAN that It can't bind multiple parameters ('JT' and 'Sales') to the request's content.

Here is my code:

[ResponseType(typeof(JT))]
public HttpResponseMessage PostJT(JT JT, Sales Sales, [FromUri] string tran)
        {
            try
            {
                if (ModelState.IsValid)
                {
                 if (tran == null)
                            {
                                return Request.CreateResponse(HttpStatusCode.Unauthorized, "Unauthorized Access!");
                            }
                            else
                            {
                                switch (tran)
                                {
                                    case "JT": db.JTs.Add(JT); 
                                        break;
                                    case "sales": db.Sales_.Add(Sales);
                                        break;
                                }
                            }
                            db.SaveChanges();
                            return Request.CreateErrorResponse(HttpStatusCode.OK, "Added!");
                        }
//below are just elses with return messages.
J.P Masangcay
  • 759
  • 2
  • 10
  • 28
  • See [this answer](http://stackoverflow.com/a/33345873/65775). You can pass multiple POST parameters to any API method by using a custom parameter binding. – Keith Oct 26 '15 at 12:41

1 Answers1

0

There is no direct way to pass multiple parameter but you can use the below work around

This link gives complete details on Web API passing Multiple parameter

    $.ajax(
    {
        url: "samples/PostAlbum",
        type: "POST",
        contentType: "application/json",
        data: JSON.stringify({ JT: jt, Sales: sales, UserToken: userToken }),
            success: function (result) {
            alert(result);
        }
    });



        [HttpPost]
        public string PostAlbum(JObject jsonData)
        {
          try
            {
                if (ModelState.IsValid)
                {
                 if (tran == null)
                            { 
                          return Request.CreateResponse(HttpStatusCode.Unauthorized,
                           "Unauthorized Access!");
                            }
                            else
                            {
                               dynamic json = jsonData;
                               JObject jtObject = json.JT;
                               JObject salesObject = json.Sales; 

                               var jt = jtObject.ToObject<JT>();
                               var sales = salesObject.ToObject<Sales>();

                                if (jt != null)
                                {
                                   db.JTs.Add(JT); 
                                }
                                 else if (sales != null)
                                {
                                    db.Sales_.Add(Sales);
                                } 
                            }
                            db.SaveChanges();
                    return Request.CreateErrorResponse(HttpStatusCode.OK, "Added!");
           }
  //below are just elses with return messages. 
        }
Rad
  • 391
  • 4
  • 14