0

I am facing problem while passing object value to Web API. I am passing value using Anuglar5

{
    "Company_User_Process_ID": 68,
    "BankName": "1860 UBL , ABBOTTABAD",
    "ChallanNo": "",
    "AccountTitle": "",
    "AccountNo": "",
    "CompanyName": "sdasd",
    "RegistrationNo": "",
    "ProcessId": 1,
    "PaymentType": false,
    "Document_Pid": "",
    "HeadsOfAccount": []
}

But I am getting Null. I got some help from This Answer. But I observed that the object that I am passing is not same as I am getting in Web API with extra {}

{
  {
    "Company_User_Process_ID": 68,
    "BankName": "1860 UBL , ABBOTTABAD",
    "ChallanNo": "",
    "AccountTitle": "",
    "AccountNo": "",
    "CompanyName": "sdasd",
    "RegistrationNo": "",
    "ProcessId": 1,
    "PaymentType": false,
    "Document_Pid": "",
    "HeadsOfAccount": []
  }
}

Here is the Code of Web API

public async Task<ResponseObject<ChallanDetailViewModel>>   CreateChallanDetail(ChallanDetailViewModel model)
 {
     ...... My code here 
 }

Here is the Model Class

public class ChallanDetailViewModel
{
    public long Company_User_Process_ID { get; set; }

    public string BankName { get; set; }

    public string ChallanNo { get; set; }

    public string AccountTitle { get; set; }

    public string AccountNo { get; set; }

    public string CompanyName { get; set; }

    public string RegistrationNo { get; set; }

    public int ProcessId { get; set; }

    public int PaymentType { get; set; }

    public string Document_Pid { get; set; }

    public List<String> HeadsOfAccount { get; set; }

}

Client Side code that is passing object

this.http.post(url, model).map((response: any) => { return 
response.json() }).toPromise();
Muhammad Usman
  • 1,366
  • 5
  • 18
  • 33

1 Answers1

-1

I'm not sure if it's the case for you. While passing a nested child list/collection to the API, the model should initialize the list in the constructor like below in order for it to pick the binding:

public class ChallanDetailViewModel
{
    public ChallanDetailViewModel()
    {
        HeadsOfAccount = new List<string>();
    }
    //Other properties
    public List<String> HeadsOfAccount { get; set; }
}
Felix Too
  • 11,614
  • 5
  • 24
  • 25