Ankit Yadav

Ankit Yadav

  • NA
  • 762
  • 36.7k

How to passt Json Object list as a parameter in Web API

May 28 2019 2:59 AM
Hey Guys , 
 
I have made an api which receives the list of the object as a paramers but the andoid developer need to pass that list as json list array , now when i pass the list as the model that i have made for the web apllication then i receive the list as null , and when i pass the same request after making another model (json to c#)with same fileds and datatypes i get the list of the model. 
  1. public class GroundWaterScheduleFormModel  //Model for Web Application   
  2.    {  
  3.        public int id { get; set; }  
  4.        public int RowNum { get; set; }  
  5.        public int? zoneId { get; set; }  
  6.        public string stateName { get; set; }  
  7.        public int? stateCode { get; set; }  
  8.        public string dateOfCensus { get; set; }  
  9.        public string districtName { get; set; }  
  10.        public int? districtCode { get; set; }  
  11.        public string blockName { get; set; }  
  12.        public int? blockCode { get; set; }  
  13.        public string villageName { get; set; }  
  14.        public int? villageCode { get; set; }  
  15.        public int SchemeSrNo { get; set; }  
  16.        public int? schemeType { get; set; }  
  17.        public int? typeOfDugWell { get; set; }  
  18.        //public int typeOfDugWell { get; set; }  
  19.        public int? typeOfTubeWell { get; set; }  
  20.   
  21.   
  22.  public class InsertGWList  
  23.    {  
  24.        public List GWFormList { get; set; }  
  25.    }  
 
  1. [Route("GroundWater/InsertGWDetails")]  
  2.       public HttpResponseMessage InsertGWMultiDetails([FromBody] InsertGWMultiList model)  
  3.       {  
  4.           try  
  5.           {  
  6.   
  7.            
  8.           }  
  9.           catch (Exception ex)  
  10.           {  
  11.               throw ex;  
  12.           }  
  13.       }  
  1. {  
  2.     "data": [{  
  3.         "id": 794,  
  4.         "RowNum": 0,  
  5.         "zoneId"null,  
  6.         "stateName"null,  
  7.         "stateCode"null,  
  8.         "dateOfCensus"null,  
  9.         "districtName""Ahmedabad",  
  10.         "districtCode": 438,  
  11.         "blockName""DHOLKA",  
  12.         "blockCode": 438006,  
  13.         "villageName""AMBARELI",  
  14.         "villageCode": 238266,  
  15.         "SchemeSrNo": 45,  
  16.         "schemeType": 2,  
  17.         "typeOfDugWell": 0,  
  18.         "typeOfTubeWell": 2  
  19.     }]   
now when i pass above request  as parameter then i get the null in InsertGWMultiListmodel , SO  i have coverted this request into Json2charpe converted and pass that model as a paramerter .
  1. public class GWDetail    //Model for android Application
  2. {    
  3.     public int id { getset; }    
  4.     public int RowNum { getset; }    
  5.     public object zoneId { getset; }    
  6.     public object stateName { getset; }    
  7.     public object stateCode { getset; }    
  8.     public object dateOfCensus { getset; }    
  9.     public string districtName { getset; }    
  10.     public int districtCode { getset; }    
  11.     public string blockName { getset; }    
  12.     public int blockCode { getset; }    
  13.     public string villageName { getset; }    
  14.     public int villageCode { getset; }    
  15.     public int SchemeSrNo { getset; }    
  16.     public int schemeType { getset; }    
  17.     public int typeOfDugWell { getset; }    
  18.     public int typeOfTubeWell { getset; }    
  19. }    
  20.     
  21. public class GWDetailList    
  22. {    
  23.     public List data { getset; }    
  24. }    
  25. }   
i am calling the api by passing GWDetailList as paramerter and i am getting the list , so my question is I dont want to creat two model for the same purpose & why i not getting the list of object in InsertGWMultiList. 
  1. [Route("GroundWater/InsertGWDetails")]    
  2.       public HttpResponseMessage InsertGWMultiDetails([FromBody] GWDetailList model)    
  3.       {    
  4.           try    
  5.           {    
  6.     
  7.              
  8.           }    
  9.           catch (Exception ex)    
  10.           {    
  11.               throw ex;    
  12.           }    
  13.       }    

Answers (1)