2

I am trying to pass data to my web API using JSON objects. Sending a single object does seems to work fine but as soon as I put a second parameter, the second object does not even seems to initialize on the server side?

Please view the code below to see how I am handling the data parameter

[HttpPost("")]
public JsonResult Post([FromBody]Log_Header headerData, 
                       [FromBody]Log_Detail detailData)
{
    return Json("Test");
}

Each of the classes above have simple string data, eg of class below:

public class Log_Header
{
    public int Id { get; set; }
    public string Name{ get; set; }
}

Example of data being sent:

var header = {
    Id: 0,
    Name: "Test 3",
}

var detail = {
    Id: 0,
    Desc: "Test 1",
}

$http({
    method: 'POST',
    url: "api/addLog",
    data : { 
        header: header,
        detail: detail
    }
})

This is all just demo data.

I have tried sending the data up in few different ways e.g below:

 var data = {
     Id: 0,
     Name: "Test 3",
     LogID: 0,
     Desc: "Test",
 }

But nothing seems to get this working, I'm guessing I am setting up the web API incorrectly?

Overall, the problem is [FromBody]Release_Log_Detail detailData does not receive any data at all and when viewing the object from a breakpoint it appears as null.

if anyone has any ideas please leave a comment or answer below. If you need anymore information from me please ask.

Naim Jamalludin
  • 195
  • 3
  • 16
furkick
  • 423
  • 6
  • 18
  • You are still posting one object, now it has two properties that are also objects. Make a c# object with two properties called header and detail of the right types and make it the input to the Post. – Mant101 Feb 16 '16 at 10:35
  • @Mant101 I have tried a lot of ways (as I said way to many to post in this question). What you have said also didn't seem to work. – furkick Feb 16 '16 at 10:36
  • Another possible duplicate [Posting to a Web API using HttpClient and Web API method \[FromBody\] parameter ends up being null](http://stackoverflow.com/q/35344981/692942) – user692942 Feb 16 '16 at 10:40
  • 1
    @Lankymart The first one you posted seems to answer my question. This can be closed as dupe for that one. Thanks for finding the post for me, must have missed that myself. – furkick Feb 16 '16 at 10:41

2 Answers2

3

Whatever we post from angular $http, it consider a single object, so we need to read it in a single object on server

we can do like this

[HttpPost("")]
public JsonResult Post([FromBody]PostData data)
{
    return Json("Test");
}

class PostData
{
   public Log_Header LogHeader { get; set; }
   public Log_Detail LogDetail { get; set; }

}

angular post

$http({
   method: 'POST',
   url: "api/addLog",
   data : { 
      LogHeader : header,
      LogDetail : detail
  }
})
Sanjay Nishad
  • 1,537
  • 14
  • 26
  • Thanks for your reply **Sanjay**! I seem to get `data` showing up as `null` using this method. How would you send through the data (via Json)? – furkick Feb 16 '16 at 11:05
  • Unfortunately the `PostData` object still grabs a `null` from the `$http post` – furkick Feb 16 '16 at 13:19
  • Oh! Plz let me know, if it's not resolved. I'll reproduce and resolve at tonight. Thanks – Sanjay Nishad Feb 17 '16 at 06:31
  • @furkick is the issue resolved? share the code if issue resolved – Ravi Dec 11 '17 at 20:32
  • @Ravi Sorry no, I ended up creating a class to hold both of the object types and passed back a single object which contained the two object types in question, the API only seemed to accept this. – furkick Dec 12 '17 at 08:30
-1

In the javascript

$http({
    method: 'POST',
    url: "api/addLog",
    data : JSON.stringify({ 
            header: header, 
            detail: detail
        })
})

create a model

public class dataViewModel
{
    public string[] header{ get; set; }
    public string[] detail{ get; set; }
}

You have create a controller of name api and action name addLog

[HttpPost]
public JsonResult addLog(dataViewModel data)
{
    return Json("Test");
}

hope this helps

anand
  • 1,559
  • 5
  • 21
  • 45