简体   繁体   中英

How to send list of object to webapi c#?

I'm trying to send a list of objects to webapi in json-array format. But, in the parameter its getting null.

Now, let me post the code that i have tried so far

[HttpPost]
        [Route("~/api/visitsave")]
        public IHttpActionResult Save(List<VisitDataModel> visitobj)
        {
            foreach (VisitDataModel visitobjs in visitobj) {
            VisitManager obj = new VisitManager(visitobjs);
            bool value = obj.Save();
        }
            return Ok();
        }

This is the json-array I'm trying to pass, but it is not working in the parameter visitobj.

Its receiving null. As I'm new to webapi and c#, I'm struggling with this.
But when i pass single json object I'm getting values and when i switched back to list, it's not working.

Let me post the json array that am trying to post:

{"visitobj":[{"Remarks":"test","UserID":193,"FindingsAtSite":"nothing","CheckInDate":"2017-02-01 12:00:00","CheckOutDate":"2017-02-01 12:00:00","VisitStatusID":1,"CreatedBy":192,"CreatedDateTime":"2017-02-01 12:00:00","Claim":{"TransportMode":1,"Date":"2017-02-01 12:00:00","FromLocation":"chennai","ToLocation":"re","Ticket":123.2,"Conveyance":123.5,"Lodge":234.0,"Meals":23}}]}

This is the jsonresponse am trying to send to my webapi can someone helpme out this may be dumb question but am struggling with this. Thanks in advance!!

This is a normal behavior since your contract does not match.

Change your parameters to the following and your argument will be ok

public class Claim
{
    public int TransportMode { get; set; }
    public string Date { get; set; }
    public string FromLocation { get; set; }
    public string ToLocation { get; set; }
    public double Ticket { get; set; }
    public double Conveyance { get; set; }
    public double Lodge { get; set; }
    public int Meals { get; set; }
}

public class Visitobj
{
    public string Remarks { get; set; }
    public int UserID { get; set; }
    public string FindingsAtSite { get; set; }
    public string CheckInDate { get; set; }
    public string CheckOutDate { get; set; }
    public int VisitStatusID { get; set; }
    public int CreatedBy { get; set; }
    public string CreatedDateTime { get; set; }
    public Claim Claim { get; set; }
}

public class VisiteRequest
{
     public List<Visitobj> visitobj { get; set; }
}

Or the second option you have to change the Json sent object as an array

[{"Remarks":"test","UserID":193,"FindingsAtSite":"nothing","CheckInDate":"2017-02-01 12:00:00","CheckOutDate":"2017-02-01 12:00:00","VisitStatusID":1,"CreatedBy":192,"CreatedDateTime":"2017-02-01 12:00:00","Claim":{"TransportMode":1,"Date":"2017-02-01 12:00:00","FromLocation":"chennai","ToLocation":"re","Ticket":123.2,"Conveyance":123.5,"Lodge":234.0,"Meals":23}}]

May be you are passing json in wrong format. I have an API action like this

[HttpPost]
[Route("sample")]
public IHttpActionResult SampleOp(List<SampleObj> smpJson)
{
      foreach (var item in smpJson){
          //Do Some Thing Here 
      }
      return ok();
}

And passing the json data as

[{
    "name":"name 1",
    "address":"address 1",
    "age":1
}, 
{
    "name":"name 2",
    "address":"address 2",
    "age":2
}]

Here is my SampleObj modal

public class SampleObj {
        public string name { get; set; }
        public string address { get; set; }
        public int age { get; set; }
    }

It is tested and working here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM