在.NET C#中传递给Web API时,Object为NULL

时间:2018-04-12 09:08:49

标签: c# asp.net-web-api2 angular5

将对象值传递给Web API时遇到问题。我正在使用Anuglar5传递价值

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

但我得到了Null。我从ASP.NET Core Diagnostics得到了一些帮助。但是我观察到我传递的对象与我在Web API中使用额外{}

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

以下是Web API代码

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

这是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; }

}

传递对象的客户端代码

this.http.post(url, model).map((response: any) => { return 
response.json() }).toPromise();

1 个答案:

答案 0 :(得分:-1)

我不确定你是否属于这种情况。在将nested child list/collection传递给API时,模型应该在构造函数中初始化列表,如下所示,以便它选择绑定:

public class ChallanDetailViewModel
{
    public ChallanDetailViewModel()
    {
        HeadsOfAccount = new List<string>();
    }
    //Other properties
    public List<String> HeadsOfAccount { get; set; }
}
相关问题