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();
14
  • content type and encoding is same ?? Commented Apr 12, 2018 at 9:16
  • While passing the object are you wrapping the object to another object? Commented Apr 12, 2018 at 9:19
  • @user1672994 No I do not. Commented Apr 12, 2018 at 9:23
  • 1
    You didn't include your asp.net model (and signature of your controller method) - so it's not much we can help with. Getting null usually means your model does not match json you post, but we don't know your model. Commented Apr 12, 2018 at 9:32
  • 1
    PaymentType is int in your model, but you are passing false (boolean) there. Either change PaymentType to bool or pass integer there ("PaymentType": 0 or something) Commented Apr 12, 2018 at 10:25

1 Answer 1

-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; }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.