title: posting object with arrays in Asp.net core Web Api
body: <p>I have SPA with react as front-end and Asp.net core web api as back-end.
  I want to post the order  and orderDetails to <code>WebApi</code> and tried to handle it using <code>OrderViewModel</code> in controller.</p>
  <p><code>edit</code>: i have found out an error and resolved it.that in <code>orderDetails.cs</code> Model <code>Qty</code> should be changed to <code>Amount</code> and tried but the same error occured screenshot also attached.</p>
  <p>the json request payload is as follows:</p>
  <pre><code>{
  Email: &quot;fiazmianwal@gmail.com&quot;
  address: &quot;1km mianwal road kuthiala sheikhan&quot;
  city: &quot;mandi bahauddin&quot;
  customerName: &quot;fiaz ahmed&quot;
  discount: 0
  mobile: &quot;03466469074&quot;
  orderDetails: [{productId: 1, productName: &quot;shalwarqamees&quot;, productCode: &quot;101&quot;, discount: 200, amount: 1,…},…]
  qty: 3
  total: 1800
  whatsapp: &quot;03466469074&quot;}
  
  </code></pre>
  <p>at the back-end the order.cs model is as:</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Linq;
  using System.Threading.Tasks;
  using System.ComponentModel.DataAnnotations;
  using System.ComponentModel.DataAnnotations.Schema;
  
  namespace WebApi.Models
  {
     
      public partial class Order
      {
          [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
          [Key]
          public int OrderId { get; set; }
          public int Qty { get; set; }
          [Required]
          [StringLength(160)]
          public string CustomerName { get; set; }
          [Required]
          [DataType(DataType.EmailAddress)]
          public string Email { get; set; }
          [Required]
          [DataType(DataType.PhoneNumber)]
          public string Mobile { get; set; }
  
          [DataType(DataType.PhoneNumber)]
          public string Whatsapp { get; set; }
          [Required]
          [StringLength(100)]
          public string City { get; set; }
        [Required]
          [DataType(DataType.MultilineText)]
          public string Address { get; set; }
          public int StoreId { get; set; }
          public long ShopId { get; set; }
          [StringLength(70)]
          public string Status { get; set; }
  
          public decimal Total { get; set; }
         
          public decimal Discount { get; set; }
          [DataType(DataType.Date)]
          [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = &quot;{0:dd/MM/yyyy}&quot;)]
          public System.DateTime UpdatedOn { get; set; }
  
      }
  }
  </code></pre>
  <p>here is the oderDetails.cs model:</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Linq;
  using System.Threading.Tasks;
  using System.ComponentModel.DataAnnotations;
  using System.ComponentModel.DataAnnotations.Schema;
  
  namespace WebApi.Models
  {
      public class OrderDetail
      {
          public int OrderDetailId { get; set; }
        
          public int ProductId { get; set; }
          public int Qty { get; set; }
          public string ProductCode { get; set; }
          public string ProductName { get; set; }
          public int StoreId { get; set; }    
          public string Unit { get; set; }
          public decimal Price { get; set; }
          public decimal Discount { get; set; }
  
          public string Currency { get; set; }
      }
  }
  
  </code></pre>
  <p>the OrderViewModel.cs in Models is as :</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.ComponentModel.DataAnnotations;
  using System.ComponentModel.DataAnnotations.Schema;
  
  namespace WebApi.Models
  {
      public class OrderViewModel
      {
          public Order Order { get; set; }
          public List&lt;OrderDetail&gt; OrderDetails { get; set; }
      }
  }
  </code></pre>
  <p>the post action method in orderscontroller is as follows:</p>
  <pre><code> [HttpPost]
          public async Task&lt;ActionResult&lt;OrderViewModel&gt;&gt; PostOrder(OrderViewModel order)
          {
  
             
           var ord = order.Order;  //save order in ord variable from orderviewmodel
              ord.UpdatedOn = DateTime.UtcNow;
              _context.Order.Add(ord); // here is null reference error
              var od = order.OrderDetails.ToList(); // get all orderdetails array of objects from orderviewmodel
              foreach (var d in od)     
              {
                  _context.OrderDetails.Add(d);  //enter all products  details  in orderdetails table
              }
              await _context.SaveChangesAsync();
  
              return CreatedAtAction(&quot;GetOrder&quot;, new { id = ord.OrderId }, order);
          }
  </code></pre>
  <p>the error is as follows:
  System.NullReferenceException: Object reference not set to an instance of an object.
  screenshot is here:
  <a href="https://i.stack.imgur.com/UYNUg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UYNUg.png" alt="screenshot of error in controller" /></a>
  The app is an online store and I want to post the order and details of products to back-end to store in the database.
  thanks in advance for workaround and help.</p>