I have an MVC application from which I want to consume an API with the structure highlighted below. However, I got an error
Object reference not set to an instance of an object
on the Web Api controller below
public IHttpActionResult Post(MyViewModel myViewModel)
{
MasterFile ord = myViewModel.MasterFile;
var ordDetails = myViewModel.LineItems;
return Ok();
}
I have structured my models as below:
public class MasterFile
{
public string payer_prov_name { get; set; }
public int roaming_amount { get; set; }
public ICollection<LineItems> LineItems { get; set; }
}
public class LineItems
{
public string prov_item_name { get; set; }
[Key]
public int item_id { get; set; }
}
public class MyViewModel
{
public MasterFile MasterFile { get; set; }
public LineItems[] LineItems { get; set; }
}
The structure of the JSON is as below - I have eliminated some fields because they are quite numerous:
(
[0] => Array
(
[payer_prov_name]
[roaming_amount]
[claim_code]
[invoices] => Array
(
[0] => Array
(
[line_items] => Array
(
[0] => Array
(
[prov_item_name]
[prov_item_code]
[payer_group_code]
[item_id]
)
[1] => Array
(
[prov_item_name]
[prov_item_code]
)
)
[payer_benefit_desc]
[payer_benefit_code]
[invoice_id]
)
)
[diagnosis] => Array
(
[0] => Array
(
[code]
[coding_standard]
)
)
[start_date]
[end_date]
)
)
)
Any help on how to structure it would be helpful