简体   繁体   English

如何将对象列表发送到WebAPI C#?

[英]How to send list of object to webapi c#?

I'm trying to send a list of objects to webapi in json-array format. 我正在尝试以json-array格式将对象列表发送到webapi。 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. 这是我要传递的json数组,但是在参数visitobj中不起作用。

Its receiving null. 它的接收为null。 As I'm new to webapi and c#, I'm struggling with this. 由于我是webapi和c#的新手,因此我为此感到困惑。
But when i pass single json object I'm getting values and when i switched back to list, it's not working. 但是当我传递单个json对象时,我正在获取值,当我切换回列表时,它不起作用。

Let me post the json array that am trying to post: 让我发布要发布的json数组:

{"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. 这是jsonresponse尝试发送给我的webapi的人,可以有人帮助我解决这个问题,但这个问题很棘手。 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 或第二个选项,您必须将Json发送的对象更改为数组

[{"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. 可能是您以错误的格式传递了json。 I have an API action like this 我有这样的API动作

[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 并将json数据作为

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

Here is my SampleObj modal 这是我的SampleObj模态

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

It is tested and working here 经过测试并在这里工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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