title: How to loop through and update multiple records at once in ASP.NET MVC Repository?
body: <p>I'm having some trouble updating records in my SQLRepository, and I'm not sure how to implement this properly or if I'm just approaching this in completely the wrong way.</p>
  
  <p>I have an invoice view which asks the user for a customer, start date and end date. Once submitted my controller then builds an IList of all the jobs which match the criteria and the view is updated via Ajax to display the list. On the same view, I have another field which asks the user to confirm these jobs are ok to invoice.</p>
  
  <p>I need this second button to then add a record to my invoice table with the invoice data - this works fine. What I also need to do is update all the job records (in the IList) to include the invoice ID number. I can't seem to get this to work.</p>
  
  <p>My updated method on the second button is below. I've tried multiple re-arrangements of this and nothing has worked. To be honest, I totally lack any sort of experience in this scenario (I'm not a developer by nature) so I'm not sure if I'm anywhere close to the mark with this.</p>
  
  <pre><code>public void SaveInvoice(Invoice invoice, IList&lt;InvoiceJob&gt; invoiceJobs)
          {
              foreach (var j in invoiceJobs)
                  {
                      InvoiceJob o = j;
                      InvoiceJob jobUpdate = (from i in invoiceJobTable where i.JobID == j.JobID select i).Single();
                      jobUpdate.InvoiceRef = invoice.InvoiceID.ToString();
                      invoiceJobTable.Attach(jobUpdate);
                      invoiceJobTable.Context.Refresh(RefreshMode.KeepCurrentValues, jobUpdate);
  
                  }
                  invoiceTable.InsertOnSubmit(invoice);
  
              invoiceTable.Context.SubmitChanges();
              invoiceJobTable.Context.SubmitChanges();
  
          }
  </code></pre>
  
  <p>The invoice table is updated fine with this method and the code runs, but the InvoiceRef is not updated for the jobs in the invoiceJobTable. It doesn't seem to touch this. As far as I can tell, the invoiceJobs IList isn't null as if I change what data the controller sends through I get null exception error on the above foreach loop.</p>
  
  <p>I hope I've explained this well enough. Any help is appreciated; I'm just a noob trying to learn. xD</p>