title: How to Insert to Master-detail tables in ASP.NET MVC 5
body: <p>I have <em>Orders</em> table and <em>OrderDetails</em> table in DB. With Entity Framework 6 I have obtained a model so I have classes generated from it. I also generated controllers and views from the Orders table. </p>
  
  <pre><code>Orders
  folio (PK)
  date
  customer (FK)
  
  OrdersDetail
  folio (FK) -- to the Orders table
  product (FK)
  price
  quantity 
  </code></pre>
  
  <p>The generated Create Post action  is the following:</p>
  
  <pre><code>[HttpPost]
          [ValidateAntiForgeryToken]
          public ActionResult Create([Bind(Include = "folio,date,customer")] Orders order)
          {
  
              if (ModelState.IsValid)
              {
                  db.Orders.Add(order);
                  db.SaveChanges();
                  return RedirectToAction("Index");
              }
  
              ViewBag.cliente = new SelectList(db.CATCTES, "COD_CTE", "NOM_CTE", order.cliente);
  
              return View(order);
          }
  </code></pre>
  
  <p>That's okay, but I would need to Insert to the other entity/table too!
  I already changed the Create.cshtml form, with the fields required for Orders table and including some kind of grid for OrdersDetail. Each row is a new entry for OrdersDetail table.</p>
  
  <p><a href="https://i.stack.imgur.com/dd0B5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dd0B5.png" alt="enter image description here"></a></p>
  
  <p><strong>The problem, is I don't know how to properly save to the other table in the Create action at Orders controller.</strong>  If this is not the right approach, tell me your comments.</p>
  
  <p>Another concern is about POSTing, How should I name the inputs to be properly grouped and use submitted values at controller? Each row is generated by this script when I click a "+" button on the form:</p>
  
  <pre><code>@section Scripts {
      @Scripts.Render("~/bundles/jqueryval")
      &lt;script&gt;
          function add_row() {
              var detalle = $('#detalle');
              var newRow = '&lt;tr&gt;' + 
                              '&lt;td data-title="Product"&gt;' +
                                '&lt;div class="input-group"&gt;' +
                                  '&lt;span class="input-group-addon"&gt;&lt;/span&gt;' +
                                    '&lt;input class="form-control" type="number" aria-label="Product" name="detail_array[].product" step="0.001" min="0.000" onchange="cambiar_importe(this)" required="true"&gt;' +
                                '&lt;/div&gt;' +
                              '&lt;/td&gt;' +
                              '&lt;td data-title="Price" &gt;' +
                                '&lt;div class="input-group"&gt;' +
                                  '&lt;span class="input-group-addon"&gt;$&lt;/span&gt;' +
                                    '&lt;input type="number" class="form-control" aria-label="Price" name="detail_array[].price" onchange="cambiar_importe(this)" step="0.001" min="0.000" required="true"&gt;' +
                                '&lt;/div&gt;' +
                              '&lt;/td&gt;' +
                              '&lt;td data-title="Quantity"&gt;' +
                                '&lt;div class="input-group"&gt;' +
                                  '&lt;span class="input-group-addon"&gt;$&lt;/span&gt;' +
                                    '&lt;input type="text" class="importe form-control" aria-label="Quantity" name="detail_array[].quantity" step="0.001" min="0.000" readonly="true" &gt;' +
                                '&lt;/div&gt;' +
                              '&lt;/td&gt;' +
                              '&lt;td data-title=""&gt;&lt;button type="button" class="btn btn-danger btn-sm" onclick="borrar_renglon(this)"&gt;Cancelar&lt;/button&gt;&lt;/td&gt;' +
                            '&lt;/tr&gt;';
  
              detalle.append(newRow);
          }
  
      &lt;/script&gt;
  }
  </code></pre>